Q7 LeetCode26 删除重复元素

1.使用快慢指针

2.slow指针代表前面的数

3.quick指针往后寻找不一样的值将slow+1替换

4.最后返回slow+1;

 1 class Solution {
 2     public int removeDuplicates(int[] nums) {
 3         if(nums.length==1){
 4             return 1;
 5         }
 6         int slowIndex=0;
 7         int quickIndex;
 8         for(quickIndex=1;quickIndex<nums.length;quickIndex++){
 9             while(nums[quickIndex]!=nums[slowIndex]){
10                 nums[slowIndex+1]=nums[quickIndex];
11                 slowIndex++;
12             }
13         }
14         return slowIndex+1;
15     }
16 }

 

posted @ 2024-06-05 22:21  清川1  阅读(22)  评论(0)    收藏  举报