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 ...
随机推荐
- jquery $(function) 区别
document.ready和onload的区别——JavaScript文档加载完成事件 页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是onloa ...
- ASP.NET中分布式事务的使用
之前发表了一篇事务的存储过程,最近在做项目的时候遇到分布式事务,所有总结一下,跟大家分享和交流一下经验.首先说明为什么要分布式事务呢?先说说我在项目的哪里遇到分布式事务吧,我是在做网站后台开发的时候, ...
- linux学习笔记之文件结构和函数
本文参考<UNIX环境高级编程> 一.基础介绍. 1:文件的构成. 1,首先声明,这里的文件和目录,表示普通的文件和目录.不确定是否可以应用到:设备,管道等特殊形式的文件(UNIX把它们也 ...
- arm mov 指令
语法格式: mov {条件} 1目的寄存器,2源操作数 作用: MOV指令可完成从另一个寄存器.被移位的寄存器或立即数赋值到目的寄存器.其中S选项为指令的操作结果是否操作CPSR中的条件标志位,当没有 ...
- ASCII、ANSI、GB2312、Unicode、UTF-8之间的关系
1.ASCII码: ASCII(American Standard Code for Information Interchange,美国信息互换标准代码)是基于拉丁字母的一套电脑编码系统.它主要用于 ...
- How to delete the icons of Win7 desktop shortcuts
1. Copy the following bat code in txt type file, 2. save it as file extension type bat, run it as ad ...
- 在WPF中自定义你的绘制(二)
原文:在WPF中自定义你的绘制(二) 在WPF中自定义你的绘制(二) ...
- 折腾Python中的Tkinter
折腾Python中的Tkinter 从oschina看到了关于Python的Tkinter简介: Tk图形用户界面 Tkinter 又从Python官网文档: Tkinter — Python int ...
- Nutch环境搭建
1. 环境准备 HOST:Ubuntu12.04LTS JDK: jdk-7u45-linux-i586.rpm Nutch:apache-nutch-1.7-bin.tar.gz Solr:solr ...
- Easy UI treegrid 分页实例
转自:http://www.jeasyuicn.com/jquery-easyui-treegird-page-processing.html