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

一: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. android中跨进程通讯的4种方式

    转自:http://blog.csdn.net/lyf_007217/article/details/8542359 帖子写的很好.看来一遍,试了一遍,感觉太有意义.必须转过来! android中跨进 ...

  2. C#中操作刚导出的Excel,设置其为自动调整列宽

    [问题] 用C#导出数据为excel,但是导出的数据中,不是自动调整列宽的: 希望变成这样的: [解决过程] 1.参考: 在C#里对excel文件的列宽进行操作 去试试: //auto adjust ...

  3. GET & POST 登录

    GET 登录 @property(nonatomic,assign)long  long hasReceivedContentLength; - (void)getLogin { NSString * ...

  4. iOS 获取系统目录

    //获取根目录 NSString *homePath = NSHomeDirectory(); NSLog(@"Home目录:%@",homePath); //获取Document ...

  5. linux下C++ STL hash_map的使用以及使用char *型变量作为Key值的一大“坑”

    计算机编程中经常会用到hash表,而在C++中,使用STL编程更是少不了的.本文将介绍STL中hash_map的使用.在hash_map中使用自定义类型作为key值的方法以及在使用char *类型作为 ...

  6. 使用 phpMailer 基于(SMTP) 发送邮件

    PHPMailer是一个用于发送电子邮件的PHP函数包.它提供的功能包括:在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址.支持多种邮件编码包括:8bit,base64,binary和quote ...

  7. humble number(hd1058)

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  8. Oracle EBS-SQL (MRP-5):重起MRP Manager.sql

    UPDATE fnd_concurrent_processes SET process_status_code = 'K' WHERE process_status_code not in ('K', ...

  9. JavaScript 获取 Div 的坐标

    示例代码: <html> <head> <script> function CPos(x, y) { this.x = x; this.y = y; } /** * ...

  10. Eclipse Useful Plugins Links

    1.maven for eclipse http://m2eclipse.sonatype.org/sites/m2e 2. svn1.6  for ecipse http://subclipse.t ...