27. Remove Element

easy

 

 

 1 class Solution {
 2     public int removeElement(int[] nums, int val) {
 3         if(nums.length == 0) return 0;
 4         if(nums.length == 1) {
 5             if(nums[0] == val) {
 6                 return 0;
 7             }else {
 8                 return 1;
 9             }
10         }
11         int i = 0, record = 0;
12         while(i < nums.length) {
13             if(nums[i] != val) {
14                 nums[record] = nums[i];
15                 record++;
16                 i++;
17             }else {
18                 i++;
19             }
20         }
21         return record;
22     }
23 }

 

posted @ 2018-09-07 22:44  jasoncool1  阅读(98)  评论(0编辑  收藏  举报