LeetCode题解:Remove Duplicates from Sorted Array I and II
来源:互联网 发布:office软件下载mac版 编辑:程序博客网 时间:2024/06/06 08:01
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 Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear onlyonce 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]
.
思路:
维护两个指针,一个指向相同元素只出现两次的已经处理过的数据部分,另一个指向现在正在处理的数据。
题解:问题2(问题1的解是问题2的特殊情况)
class Solution {public: int removeDuplicates(int A[], int n) { if (n <= 1) return n; int iter = 0; int unique_iter = 0; int curr_value = A[0]; int curr_count = 1; for(iter = 1; iter < n; ++iter) { if (A[iter] == curr_value) { // more than 2 occurance? if (++curr_count > 2) continue; } else { curr_value = A[iter]; curr_count = 1; } A[++unique_iter] = curr_value; } return unique_iter + 1; }};
- LeetCode题解:Remove Duplicates from Sorted Array I and II
- Leetcode: Remove Duplicates from Sorted Array I and II
- LeetCode Remove Duplicates from Sorted Array I and II
- Leetcode Remove Duplicates from Sorted Array I and II
- LeetCode 题解(162): Remove Duplicates from Sorted Array II
- leetcode题解-80. Remove Duplicates from Sorted Array II
- leetcode题解-80 Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array I&&II
- 【LeetCode】Remove Duplicates from Sorted Array I && II
- LeetCode Solutions : Remove Duplicates from Sorted Array I & II
- LeetCode - Remove Duplicates from Sorted Array I && II
- [leetcode题后感] Remove Duplicates from Sorted Array I、 II
- leetcode-26&80 Remove Duplicates from Sorted Array I&II
- 【LeetCode从零单刷】Remove Duplicates from Sorted Array I & II
- [LeetCode]26. Remove Duplicates from Sorted Array I&II
- [Leetcode] #26#80 Remove Duplicates from Sorted Array I & II
- LeetCode:Remove Duplicates from Sorted Array I & II
- leetcode-26&80 Remove Duplicates from Sorted Array I & II
- LeetCode题解:Clone Graph
- Generate Parentheses 产生所有括号组合 @LeetCode
- Linux Ramdisk启动方式下文件系统兼容性快速排查。
- 企业软件管理FileWave
- const char * ptr;char * const cp 的区别
- LeetCode题解:Remove Duplicates from Sorted Array I and II
- hdoj1106 ( strtok函数 )
- OSX: 切换10.9用户桌面壁纸的脚本
- 推荐一个git源码托管网站
- c对象创建
- 利用开源赚钱的九项秘诀
- create bubble btns
- 几个Android 4.4 kitkat 同步地址(aosp、slim、omni等)
- 软粒子