leecode-Remove Duplicates from Sorted Array
来源:互联网 发布:软件安装在系统盘 编辑:程序博客网 时间:2024/06/05 02:27
1. 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 = [1,1,2] ,
Your function should return length = 2, and A is now [1,2] .
代码
#include<iostream>#include<vector>using namespace std;class solution {public: int removeDuplicates(vector<int> &nums) { if (nums.empty()) return 0; int index = 1; for (int i = 1; i< nums.size(); i++) { if (nums[i] != nums[index - 1]) nums[index++] = nums[i]; } return index; }};void main(){ vector<int> nums = { 1,1,2,2,4 }; for (int i = 0; i< nums.size(); i++) cout << nums[i] << " "; cout << endl; solution sol; int length = sol.removeDuplicates(nums); for (int i = 0; i < length; i++) cout << nums[i] << " "; cout << endl;}
2. 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]
class solution {public: int removeDuplicates(vector<int> &nums) { if (nums.size() <= 2) return nums.size(); int index = 2; for (int i = 2; i< nums.size(); i++) { if (nums[i] != nums[index - 2]) nums[index++] = nums[i]; } return index; }};
0 0
- LeeCode-Remove Duplicates from Sorted Array II
- leecode-Remove Duplicates from Sorted Array
- Leecode-Remove Duplicates from Sorted Array
- leecode 解题总结:26 Remove Duplicates from Sorted Array
- leecode 解题总结:80. Remove Duplicates from Sorted Array II
- Leecode Remove Duplicates from Sorted List
- LeeCode-Remove Duplicates from Sorted List
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Hibernate关系映射之多对多映射的两种方式
- ElasticSearch学习23_CENTOS安装ElasticSearch中don't run elasticsearch as root
- 红黑树
- 深度学习与卷积神经网络(直观理解)
- C++ priority_queue(优先队列)使用
- leecode-Remove Duplicates from Sorted Array
- UE4实现按键双击检测
- 【脚本语言系列】关于Python调试报错,你需要知道的事
- 十五周项目1
- 关于Ubuntu上Vim如何复制所有文本并粘贴到外部
- win10下msadodc.ocx、mscomm32.ocx、MSDATLST.ocx等OCX控件的安装注册方法步骤
- C语言如何生成规定范围内的随机数
- Arduino学习笔记(1)
- CodeForces-743C Vladik and fractions(构造)