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 ...
随机推荐
- Deppon接口开发
一.1) 支持的传输协议 http ,暂时只支持HTTP协议进行通信. (2) 支持的数据传输格式 Json ,所有接口暂只支持json消息格式. (3) 编码格式:UTF-8 交互编码格 ...
- Model注解的后台原理
Asp.net MVC的验证特性是由模型绑定器.模型元数据.模型验证器和模型状态组成的协调系统的一部分. 1.验证和模型绑定 默认情况下,Asp.net MVC框架在模型绑定石执行验证逻辑,在操作方法 ...
- session的存储方式和配置
Session又称为会话状态,是Web系统中最常用的状态,用于维护和当前浏览器实例相关的一些信息.我们控制用户去权限中经常用到Session来存储用户状态,这篇文章会讲下Session的存储方式.在w ...
- C - The Hardest Problem Ever
Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever ...
- rpm方式安装MySQL-5.6
1. 卸载系统原有的mysql-libs rpm -e [name] --nodeps 2. 安装MySQL rpm -ivh MySQL-server-XXXXXX rpm -ivh MySQL-c ...
- 设定MS SQL Server 2008定期自动备份
1.说明 SQL Server2008 本身具有定期自动备份功能,我们只需要通过简单的配置就可以实现非常简单高效的自动备份功能. 2.打开SQL Server代理服务 要实现自动备份功能,首先要保证S ...
- Mysql 记录
1.创建用户命令: <!---->mysql> CREATE USER yy IDENTIFIED BY '123'; yy表示你要建立的用户名,后面的123表示密码 上面建立的用户 ...
- PreparedStatement设置时间
程序代码里面需要用PreparedStatement来设置时间过滤参数,时间参数中带有时分秒,用ps.setDate来设置的时候,会丢失时间部分,只有日期,用setTimestamp来设置参数,既有日 ...
- jquery中attr()与prop()函数用法实例详解(附用法区别)
本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...
- netty实现消息转发服务
1.结构图 2.消息服务器 消息服务器(SNS)由Http Netty Server(HNS)和WebSocket Netty Server(WNS)组成.HNS采用Netty Http+XML协议栈 ...