<LeetCode OJ> 26 / 80 Remove Duplicates from Sorted Array(I / II)
来源:互联网 发布:矩阵lu分解问题 编辑:程序博客网 时间:2024/06/06 14:15
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.
双指针问题,用len维护当前所有不重复的元素,用i遍历当前元素是否与相邻的元素相等!如果相等什么都不做,如果不相等则修改nums[len++]为当前这个元素。
class Solution {public: int removeDuplicates(vector<int> &nums) { if(nums.size() < 2) return nums.size(); int len = 1; for(int i = 1; i < nums.size(); i++){ if(nums[i] != nums[i - 1]) nums[len++] = nums[i]; } return len; }};
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.
分析:
一个道理,双指针,一个指针始终指向当前安全的符合要求的序列,另一个遍历数组
class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size()<=2) return nums.size(); int len=1,cnt=1; for(int j=1;j<nums.size();j++) { if(nums[j]==nums[j-1]) cnt++; else cnt=1; if(cnt < 3) nums[len++] = nums[j]; } return len; }};
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50499722
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895
- leetcode-26&80 Remove Duplicates from Sorted Array I&II
- [Leetcode] #26#80 Remove Duplicates from Sorted Array I & II
- leetcode-26&80 Remove Duplicates from Sorted Array I & II
- <LeetCode OJ> 26 / 80 Remove Duplicates from Sorted Array(I / II)
- LeetCode OJ:Remove Duplicates from Sorted Array II
- LeetCode OJ - Remove Duplicates from Sorted Array II
- LeetCode OJ Remove Duplicates from Sorted Array II
- LeetCode 26, 80, 81, 82, . Remove Duplicates from Sorted Array/ List i, ii
- LeetCode:Remove Duplicates from Sorted Array I&&II
- LeetCode题解:Remove Duplicates from Sorted Array I and 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 and II
- LeetCode - Remove Duplicates from Sorted Array I && II
- [leetcode题后感] Remove Duplicates from Sorted Array I、 II
- LeetCode Remove Duplicates from Sorted Array I and II
- 【LeetCode从零单刷】Remove Duplicates from Sorted Array I & II
- Leetcode Remove Duplicates from Sorted Array I and II
- Android照片墙完整版,完美结合LruCache和DiskLruCache
- Mac 显示文件夹下隐藏文件
- vector
- Eclipse快捷键 10个最有用的快捷键
- Android多点触控技术实战,自由地对图片进行缩放和移动
- <LeetCode OJ> 26 / 80 Remove Duplicates from Sorted Array(I / II)
- iPhone全部设备分辨率速查
- 结构型-适配器adapter
- Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
- 2016年的学习计划
- 一套通过c# sap-rfc 完整处理内表输入输出的程序
- 【java】request.getParameter 和 request.getAttribute 的功能作用详解.
- linux + cuda7.5 + opencv2.4.8编译caffe
- 数据在内存中的存放