【leetcode刷题笔记】Remove Duplicates from Sorted Array II

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].



题解:

设置两个变量:右边kepler和前向游标forward。如果当前kepeler所指的元素和它下一个元素相等,那么向A中存入这两个相等的元素,然后将forward置为kepeler+2,向前移动,如果碰到的元素都跟当前kepeler所指的元素相等,就忽略,否则,将kepeler移向forward所指元素,继续循环;如果当前kepeler所指的元素和它下一个元素不相等,那么就把这个元素直接加入A中,kepeler前移,继续循环。

例如题目中给的数组:1,1,1,2,2,3.

初始kepeler指向第一个1,发现kepeler+1也是一个1,就把forward置为kepeler+2=2,向前探测。探测到index为3的元素时候,发现和kepeler所指的元素不相等,就把kepeler移到索引为3处,继续上述过程,最终得到数组1,1,2,2,3.

代码如下:

 1 public class Solution {
 2     public int removeDuplicates(int[] A) {
 3         if(A == null || A.length == 0)
 4             return 0;
 5         int kepeler = 0,forward = 0;
 6         int NewSize = 0;
 7
 8         while(kepeler < A.length){
 9             if(kepeler+1 < A.length && A[kepeler+1] == A[kepeler]){
10                 A[NewSize++] = A[kepeler];             //相同的元素有两个,都放入数组中
11                 A[NewSize++] = A[kepeler];
12                 forward = kepeler+2;
13                 while(forward < A.length && A[forward] == A[kepeler])
14                     forward++;
15                 kepeler = forward;
16             }
17             else if(kepeler < A.length){
18                 A[NewSize++] = A[kepeler];
19                 kepeler++;
20             }
21         }
22         return NewSize;
23     }
24 }

【leetcode刷题笔记】Remove Duplicates from Sorted Array II,布布扣,bubuko.com

时间: 2024-10-27 08:20:51

【leetcode刷题笔记】Remove Duplicates from Sorted Array II的相关文章

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 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次以上出现的数,但是可以允许重复2次

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: 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

LeetCode: Remove Duplicates from Sorted Array II [080]

[题目] 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]. [题意] 给定一个有序数组,给数组去重,和Remove Duplicates fro

leetcode_80题——Remove Duplicates from Sorted Array II(两个指针)

Remove Duplicates from Sorted Array II Total Accepted: 38480 Total Submissions: 125235My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,

[LeetCode][JavaScript]Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II 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 n

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