Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置
一:Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i = ;
int n = nums.size();
while(i<n){
if(nums[i]==val){
swap(nums[i],nums[--n]);
}else{
++i;
}
}
nums.erase(nums.begin()+i,nums.end());
return nums.size();
}
};
二: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) {
int numsSize = nums.size();
int j=;
int repeat = ;
for(int i=;i<numsSize;i++){
if(i==){
j=;
repeat = nums[i];
}else{
if(nums[i]!=repeat){
nums[j++] = nums[i];
repeat = nums[i];
}
}
}
for(int i=numsSize-;i>=j;i--){
nums.erase(nums.begin()+i);
}
return j;
}
};
三: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.
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int numsSize = nums.size();
int j=;
int repeat = ;
int repeatTime = ;
for(int i=;i<numsSize;i++){
if(i==){
j=;
repeat = nums[i];
repeatTime = ;
}else{
if(nums[i]!=repeat){
nums[j++]=nums[i];
repeat = nums[i];
repeatTime = ;
}else if(repeatTime < ){
nums[j++]=nums[i];
repeatTime ++;
}
}
}
for(int i=numsSize-;i>=j;i--){
nums.erase(nums.begin()+i);
}
return j;
}
};
Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II的更多相关文章
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [array] leetCode-27. Remove Element - Easy
27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...
- LeetCode Array Easy 27. Remove Element 解题
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- [LeetCode] Remove Element 移除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- [LeetCode] Remove Element 分析
Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- Jq合成事件绑定
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- pl sql练习(3)
1.s树形结构查询表中的数据:比如emp表中每个员工都有自己的头,即公司中的职位是按层次划分的,类似一个树,因此有时需要按层次显示查询的结果. select empno,mgr,ename,job f ...
- Thinking In Java读书笔记--对象导论
Thinking In Java读书笔记--对象导论[对象]服务提供者==>将对象看做一个服务提供者[程序员分类][类创造者]/[客户端程序员] [访问控制存在的原因?][1]客户端程序员无法触 ...
- Spring-----8、深入理解容器中的bean
转载自:http://blog.csdn.net/hekewangzi/article/details/45648687
- linux学习笔记之系统标准:POSIX,ISO C...
一.POSIX,ISO C,Single UNIX Specification的概念. 1,POSIX:Portable Operating System Interface.可移植操作系统接口.期望 ...
- 从汇编看c++对静态成员的存取
c++中静态成员变量不存在于对象之中,而存在于全局数据段,只是其可见性受到限制,仅能被所属类访问,而非静态成员变量存在于对象中,因而,在访问两种不同数据成员时,会有些许差别.对于静态数据成员的访问,是 ...
- rotate.js实现图片旋转 (chrome,IE,firefox都可以实现)
找了好多资料,要么是IE可以用,但是谷歌不行,,还有就是两个都可以用的,图片大小显示不全.终于找到一个好一点的js,先贴一下代码. 1.rotate.js jQuery.fn.rotate = fun ...
- jQuery 方法
方法 描述 animate() 对被选元素应用"自定义"的动画 clearQueue() 对被选元素移除所有排队函数(仍未运行的) delay() 对被选元素的所有排队函数(仍未运 ...
- django之uWSGI配置 +Nginx
参考文档 官方文档 安装: pip install uwsgi 启动命令: 方法一.直接命令启动 /home/zabbix/application/python/bin/uwsgi --socke ...
- java学习:AWT组件和事件处理的笔记(1)--Frame
1.java的抽象窗口工具包(AWT)中包含了许多类来支持GUI设计2.AWT由java的java.awt包提供3.再进行GUI编程时,要理解:容器类(Container),组件(component) ...