Remove Duplicates From Sorted Array leetcode java

算法描述:

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.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn‘t matter what you leave beyond the new length.

算法分析:一个有序数组,出去其中出现次数大于1的数,返回剩余元素个数

代码:设置两个指针,i 遍历原数组,j 是去除重复后的下标

public int removeDuplicates(int[] nums) {
        if(nums == null || nums.length == 0)
            return 0;
        int len = nums.length;
        int j = 1; //数组第一个元素是不需要变的,所以长度至少为1
        for(int i = 1; i < len; i++){ //数组遍历从下标1开始
            if(nums[i] == nums[i - 1])
                continue;
            else {
                nums[j++] = nums[i];
            }
        }

        return j;
    }
时间: 2024-10-11 07:51:27

Remove Duplicates From Sorted Array leetcode java的相关文章

Remove Duplicates from Sorted List leetcode java

题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 题解: 这道题是经典的双指针问题,用两个指针一前一后指向链表.如果两个指针指向的值相等,那么就让第二个指针一直

leetcode 80 Remove Duplicates from Sorted Array II ----- java

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

Remove Duplicates from Sorted Array -- leetcode

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. For example, Given input array A = 

leetCode-数组:Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素 考察数组的基本操作: class Solution { public int removeDuplicates(int[] nums) { if (nums==null || nums.length==0) return 0; int index = 1; for(int i =1; i<nums.length; i++){ if(nums[i]!=nums[i-1]){ nums[index]

Remove Duplicates from Sorted Array II leetcode java

题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 之前是不允许有重复的. 现在是可以最多允许2个一样的元素. 然后删除dupli

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

26. 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 mem

【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)

leetcode上有四道关于移除有序序列中重复数字的题目,其中两道为数组结构,两道为链表结构,分别为: (1)Remove Duplicates from sorted array I:移除一个有序数组中的重复数字,并且返回新数组的大小. (2)Remove Duplicates from sorted array II:移除一个有序数组中的重复数字,并且返回新数组的大小,和上道题目不同的是每个数字可以最多重复两次出现. (3)Remove Duplicates from sorted list

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {