leetcode 26|27|80. Remove Duplicates from Sorted Array 1|2 && 27.Remove Element
来源:互联网 发布:淘宝客服主管提成方案 编辑:程序博客网 时间:2024/04/25 12:21
26. 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 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.
方法一:删除重复
方法二:不删除元素,而是把不重复的元素放到前面去。
class Solution {public: int removeDuplicates(vector<int>& nums) { //way-1 /* for(int i=0;i<nums.size();i++) { if(nums[i]==nums[i+1] && i+1<nums.size()) { nums.erase(nums.begin()+i+1); i--; } } return nums.size(); */ //way-2 if(nums.size()<2) return nums.size(); int j=0; for(int i=1;i<nums.size();i++) { if(nums[i]!=nums[j]) nums[++j]=nums[i]; } return j+1; }};
27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.
方法一:移除指定值的元素
方法二:交换,因为好像是要求不能删除
class Solution {public: int removeElement(vector<int>& nums, int val) { //way-1 /* for(int i=0;i<nums.size();i++) { if(nums[i]==val) { nums.erase(nums.begin()+i); i--; } } return nums.size(); */ //way-2 int j=0; for(int i=0;i<nums.size();i++) { if(nums[i]!=val) { nums[j]=nums[i]; j++; } } return j; }};
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.
两个指针,pos左边存符合的,(pos,r)存不符合的,r之后存没判定的!
这种题就要想两个指针移动这种方法。就像之前的 sort colors。
class Solution {public: int removeDuplicates(vector<int>& nums) { if (nums.size() == 0) return 0; int pos = 0; //pos左边存符合的,(pos,r)存不符合的,r之后存没判定的 int flag = 0; for (int r = 1; r < nums.size(); r++) { if (nums[r] != nums[pos]) { swap(nums[r], nums[++pos]); flag = 0; } else if (flag == 0) { swap(nums[r], nums[++pos]); flag++; } } return pos+1; }};
- leetcode 26|27|80. Remove Duplicates from Sorted Array 1|2 && 27.Remove Element
- Leetcode 27. Remove Element & 26. Remove Duplicates from Sorted Array
- [LeetCode]27. Remove Element【&26. Remove Duplicates from Sorted Array】
- [LeetCode] Remove Element、Remove Duplicates from Sorted Array、Remove Duplicates from Sorted Array II
- LeetCode OJ - Remove Duplicates from Sorted Array & Remove Element
- Leetcode:Remove Duplicates from Sorted Array与Remove Element
- leetcode Remove Duplicates from Sorted Array & Remove Element
- [LeetCode] Remove Duplicates from Sorted Array & Remove Element
- 26. Remove Duplicates from Sorted Array&&27. Remove Element
- LeetCode(26)Remove Duplicates From Sorted Array
- [leetcode 26] Remove Duplicates from Sorted Array
- [Leetcode] 26 - Remove Duplicates from Sorted Array
- leetcode|26|Remove Duplicates from Sorted Array
- leetcode-26 Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- 实时进行GZIP压缩优化Asp.Net页面的CompressionModule对Asp.Net Ajax及搜索引擎的兼容性测试!
- UDP广播遇到的坑
- webpack打包vue2.0项目时必现问题。
- 数据提交及弹出框样式
- Touch_Java 10:输入、输出流
- leetcode 26|27|80. Remove Duplicates from Sorted Array 1|2 && 27.Remove Element
- 坦克大战,地图编辑
- 抽象数据类型-线性结构-先进先出:队列
- SpringCloud Zuul修改请求参数信息
- Django 自定义过滤器 笔记
- 十-1 检查和调试JavaScript 如何设置断点
- 整合企业Spring Cloud分布式微服务云架构构建
- U3D Invoke() IsInvoking CancelInvoke方法的调用
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)