lintcode-easy-Remove Duplicates from Sorted Array

Posted 哥布林工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-easy-Remove Duplicates from Sorted Array相关的知识,希望对你有一定的参考价值。

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

 

public class Solution {
    /**
     * @param A: a array of integers
     * @return : return an integer
     */
    public int removeDuplicates(int[] nums) {
        // write your code here
        
        if(nums == null)
            return 0;
        if(nums.length <= 1)
            return nums.length;
        
        int i = 0;
        int j = 1;
        
        while(j < nums.length){
            while(j < nums.length && nums[j] == nums[i])
                j++;
            
            if(j < nums.length){
                i++;
                nums[i] = nums[j];
            }
        }
        
        return i + 1;
    }
}

 

以上是关于lintcode-easy-Remove Duplicates from Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章

lintcode-easy-Remove Linked List Elements

lintcode-easy-Remove Duplicates from Sorted List

lintcode-easy-Remove Nth Node from End of List

根据 R 中的条件创建重复行

mysql常见错误

jar文件配置冲突问题transformResourcesWithMergeJavaResForDebug