以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置

一: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 1122 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的更多相关文章

  1. 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 ...

  2. [array] leetCode-27. Remove Element - Easy

    27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...

  3. 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 ...

  4. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  5. [LeetCode] 27. Remove Element 移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  6. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  7. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  8. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. 用MVC4+EF改写XXX系统的计划--前言

    感觉自己工作了三年,重来没有自己一个人写一个项目,从开始的策划,功能需求,业务逻辑,扩展,性能优化等等方面去做,从今天起准备发比半年时间重写XXX项目,每天中午和晚上分别花半个小时和一个小时开发,周末 ...

  2. 手把手教你使用Git(转)

    Git使用教程 2014-10-25 14:29 by 云溪0707, 10532 阅读, ... 评论, 收藏, 编辑 Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系 ...

  3. css模板之 web模板一

    效果 <html><head><style type="text/css">div.container{width:100%;margin:0p ...

  4. PHP_CURL请求教程, 内含简单粗暴curl

    //curl访问 //需要url或者data //返回的数组是JSON数据形式 function ppd_curl($url,$data = null){ //\Think\Log::record($ ...

  5. make clean与make distclean的区别

    make clean仅仅是清除之前编译的可执行文件及配置文件. 而make distclean要清除所有生成的文件. Makefile 在符合GNU Makefiel惯例的Makefile中,包含了一 ...

  6. wordpress一些常用代码

    显示最新文章 <div id="newpost"> <h2> 最新文章</h2> <?php $previous_posts = get_ ...

  7. 用户登录之cookie信息安全一二事

    大家都知道用户登陆后,用户信息一般会选择保存在cookie里面,因为cookie是保存客户端, 并且cookie可以在客户端用浏览器自由更改,这样将会造成用户cookie存在伪造的危险,从而可能使伪造 ...

  8. Nginx 配置指令的执行顺序(九)

    紧接在 server-rewrite 阶段后边的是 find-config 阶段.这个阶段并不支持 Nginx 模块注册处理程序,而是由 Nginx 核心来完成当前请求与 location 配置块之间 ...

  9. 诺基亚 Lumia 1020的价格

       大部分的中国人心中,都有一个诺基亚情节.经典的1110,耐摔程度强得惊人,相信很多人都知道它的厉害.    虽说这些年诺基亚由于没有跟上Android这一波智能机的浪潮,在智能机时代被三星苹果等 ...

  10. Zigbee、WiFi和433MHz无线技术各有特点

    Zigbee.WiFi和433MHz无线技术都属于近距离无线通讯技术,并且都使用ISM免执照频段,但它们各具特点.   ZigBee的特点是低功耗.高可靠性.强抗干扰性,布网容易,通过无线中继器可以非 ...