删除有序数组中的重复项 (Remove Duplicates from Sorted Array)

 

思路:

// @Title: 删除有序数组中的重复项 (Remove Duplicates from Sorted Array)
// @Author: qisiii
// @Date: 2021-08-13 09:00:32
// @Runtime: 1 ms
// @Memory: 39.8 MB
// @comment: 
// @flag: 
class Solution {
    public int removeDuplicates(int[] nums) {
        
        if(nums.length<=1){
            return nums.length;
        }
        int count=1;
        for(int i=1;i<nums.length;i++){
                if(nums[i]==nums[i-1]){
                    continue;
                }
                
                nums[count]=nums[i];
                count++;
        }
        return count;
    }
}

思路:

// @Title: 删除有序数组中的重复项 (Remove Duplicates from Sorted Array)
// @Author: qisiii
// @Date: 2022-02-26 12:22:32
// @Runtime: 0 ms
// @Memory: 42.9 MB
// @comment: 
// @flag: 
class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length<=1){
return nums.length;
        }
        int repeat=0;
        //先找到第一个重复的位置
        for(int i=1;i<nums.length;i++){
            if(nums[i]==nums[i-1]){
                repeat=i;break;
            }
        }
        if(repeat==0){
            return nums.length;
        }
        //当不等的时候,覆盖指针的值,同时指针右移;
        for(int j=repeat+1;j<nums.length;j++){
            if(nums[j]!=nums[j-1]){
                nums[repeat]=nums[j];
                repeat++;
            }
        }
        return repeat;
    }
}

+++ title = “删除有序数组中的重复项 (Remove Duplicates from Sorted Array)” draft = false +++

思路:

// @Title: 删除有序数组中的重复项 (Remove Duplicates from Sorted Array)
// @Author: qisiii
// @Date: 2024-04-13 18:28:28
// @Runtime: 0 ms
// @Memory: 43.9 MB
// @comment: 
// @flag: 
class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length<=1){
            return nums[0];
        }
        int left=0,right=left+1;
        while(right<nums.length){
            if(nums[left]!=nums[right]){
                nums[++left]=nums[right];
            }
            right++;
        }
        return left+1;
    }
}

+++ title = “删除有序数组中的重复项 (Remove Duplicates from Sorted Array)” draft = false +++

思路:

// @Title: 删除有序数组中的重复项 (Remove Duplicates from Sorted Array)
// @Author: qisiii
// @Date: 2021-08-16 13:09:44
// @Runtime: 1 ms
// @Memory: 39.7 MB
// @comment: 
// @flag: 
class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length<=1){
            return nums.length;
        }
        int max=1;
        for (int i = 1; i < nums.length; i++) {
            int num = nums[i];
            if (nums[i]!=nums[i-1]){
                nums[max]=nums[i];
                max++;
            }
        }
        return max;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18