java 26.从Sorted Array.java中删除重复项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 26.从Sorted Array.java中删除重复项相关的知识,希望对你有一定的参考价值。

"""
Testcases:
Input:
[]
[1,1,2]
[1,1,1]
[0]

Output:
[]
[1,2]
[1]
[0]

"""
class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        
        
        if not nums:
            return 0;
            
        res = 0;  # res is used to maintain the n th element in nums without repetition
        # res starts from 0;
        
        for i in range(1,len(nums)):
            
            if nums[i] != nums[res]:
                res += 1;
                nums[res] = nums[i];
                
        return res + 1;
public class Solution {
    public int removeDuplicates(int[] nums) {
        int m = nums.length;
        int res = 0;
        for(int i = 1; i < m; i++){
            if(nums[i] != nums[res]){
                nums[++res] = nums[i];
            }
            
        }
        return res + 1;
    }
}

以上是关于java 26.从Sorted Array.java中删除重复项的主要内容,如果未能解决你的问题,请参考以下文章

java 26.从Sorted Array.java中删除重复项

java 26.从Sorted Array.java中删除重复项

java 26.从Sorted Array.java中删除重复项

java 26.从Sorted Array.java中删除重复项

26. Remove Duplicates from Sorted Arrayleetcode,数组,array,java,算法

python 26从Sorted Array.py中删除重复项