LeetCode 80

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 nums being 1, 1, 2, 2 and 3.
It doesn‘t matter what you leave beyond the new length.

 1 /*************************************************************************
 2     > File Name: LeetCode080.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Tue 17 May 2016 20:54:02 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Remove Duplicates from Sorted Array II
11
12     Follow up for "Remove Duplicates":
13     What if duplicates are allowed at most twice?
14
15     For example,
16     Given sorted array nums = [1,1,1,2,2,3],
17
18     Your function should return length = 5,
19     with the first five elements of nums being 1, 1, 2, 2 and 3.
20     It doesn‘t matter what you leave beyond the new length.
21
22  ************************************************************************/
23
24 #include <stdio.h>
25
26 int removeDuplicates(int* nums, int numsSize)
27 {
28     if(numsSize < 2)
29     {
30         return numsSize;
31     }
32
33     int count = 1;
34     int j = 1;
35     int i;
36
37     for( i=1; i<numsSize; i++ )
38     {
39         if( nums[i] == nums[i-1] )
40         {
41             count ++;
42             if( count < 3 )
43             {
44                 nums[j++] = nums[i];
45             }
46             else
47             {
48                 printf("***\n");
49             }
50             printf("i=%d j=%d nums[i]=%d nums[j]=%d\n", i,j,nums[i],nums[j]);
51             printfNums(nums,numsSize);
52         }
53         else
54         {
55             nums[j++] = nums[i];
56             count = 1;
57             printf("i=%d j=%d nums[i]=%d nums[j]=%d\n", i,j,nums[i],nums[j]);
58             printfNums(nums,numsSize);
59         }
60     }
61     return j;
62
63 }
64
65 void printfNums( int* nums, int numsSize )
66 {
67     int i;
68     for( i=0; i<numsSize; i++ )
69     {
70         printf("%d ", nums[i]);
71     }
72     printf("\n");
73 }
74
75 int main()
76 {
77     int nums[] = { 1, 2, 3, 3, 3, 5, 5, 5 };
78     int numsSize = 8;
79
80     int ret = removeDuplicates( nums, numsSize );
81     printf("%d\n", ret);
82 }
时间: 2024-08-13 19:40:26

LeetCode 80的相关文章

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) {

LeetCode 80. 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 nums being 1, 1, 2, 2 and 3. It doesn't

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

[leetcode] 80 Remove Duplicates from Sorted Array II(数组下标操作)

因为这道题目的题意是要求我们在原数组上进行操作,所以操作变得稍微复杂了些,否则直接使用map最为简单. 基本思想是记录两个指针,一个是当前数组,另一个是目的数组,注意如果发现重复数超过2,那么目的数组的cur就要阻塞, 直到不同的出现后再赋值前进. class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0) return 0; int cur=1; //修改后数组的下标点 i

leetcode[80] Remove Duplicates from Sorted Array II

给定一个排好序的数组,要求里面数字重复的次数不超过2,并且记录在原数组的前头,返回剩余长度.例如给定: A = [1,1,1,2,2,3] 返回 5,并且A = [1,1,2,2,3] 思路: 用till记录满足条件的下一个位置,以便下一次填入 用repeat记录重复的次数,超过2则不理,否则往till里记录 如果n为0,则返回0,如果非空,那么第一个数肯定是符合的,所以till从1开始,repeat从1开始. class Solution { public: int removeDuplica

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

LeetCode 80: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 nums being 1, 1, 2, 2 and 3. It doesn'

[LeetCode]80. Merge Two Sorted Lists合并两个排序链表

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Subscribe to see which companies asked this question 解法1:递归.首先比较头节点大小,若l2->val>l1->val,则返回mergeTwoLists(

leetcode || 80、Remove Duplicates from Sorted Array II

problem: 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]. Hide Tags Array Two Pointers 题意:对数组进行去