26. Remove Duplicates from Sorted Array

别想复杂 就一个记录标的 一个记录要对比的

 

 

 

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

 

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