Leetcode | Remove Duplicates from Sorted Array I && II

Remove Duplicates from Sorted Array I


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 = [1,1,2],

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

这道题很简单,就是维护一个新数组的尾指针,tail,把去重后的数放到A[tail]就行,最终返回tail。


 1 class Solution {
2 public:
3 int removeDuplicates(int A[], int n) {
4 if (n <= 1) return n;
5 int tail = 0, v;
6
7 for (int i = 0; i < n;) {
8 v = A[i];
9 for (i++; i < n && A[i] == v; ++i);
10 A[tail++] = v;
11 }
12 return tail;
13 }
14 };

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

同上,区别在于,去重的时候,允许有两个重复的数;多的话还是略掉。


 1 class Solution {
2 public:
3 int removeDuplicates(int A[], int n) {
4 if (n <= 1) return n;
5 int tail = 0, v, j = 0;
6
7 for (int i = 0; i < n;) {
8 v = A[i];
9 for (j = 0; j < 2 && i + j < n && A[i + j] == v; ++j) {
10 A[tail++] = v;
11 }
12 for (i += j; i < n && A[i] == v; ++i);
13 }
14 return tail;
15 }
16 };

哈哈,Leetcode第一遍至此结束了!!!!cheers!

Leetcode | Remove Duplicates from Sorted Array I &&
II,布布扣,bubuko.com

Leetcode | Remove Duplicates from Sorted Array I &&
II

时间: 2024-10-11 13:22:52

Leetcode | Remove Duplicates from Sorted Array I && II的相关文章

[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 [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:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

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

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 [025]

[题目] 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

Leetcode | Remove Duplicates from Sorted List I &amp;&amp; II

Remove Duplicates from Sorted List I 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] Remove Duplicates from Sorted Array [19]

题目 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 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 从已排序的数组中删除重复元素

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 from sorte