27. Remove Element(双指针)
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length =5, with the first five elements ofnumscontaining0,1,3,0, and 4. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length. 法1: 统计出val的数量,把val 交换到后面。
class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
int j = nums.length-1;
int cnt = 0;
for(int k =0;k<nums.length;k++)
if(nums[k]==val)
cnt++;
if(cnt==0)
return nums.length;
while(i<nums.length-cnt||j>nums.length-cnt){
while(i<nums.length-cnt&&nums[i]!=val) i++;
while(j>nums.length-cnt&&nums[j]==val) j--;
swap(nums,j,i);
}
return nums.length-cnt;
}
private void swap(int[] a,int i ,int j){
int t = a[i];
a[i] = a[j];
a[j] =t;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
int m = 0;
for(int i = 0;i<nums.length;i++){
if(nums[i]!=val){
nums[m] = nums[i];
m++;
}
}
return m;
}
}
27. Remove Element(双指针)的更多相关文章
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- [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 ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- C# 写 LeetCode easy #27 Remove Element
27. Remove Element Given an array nums and a value val, remove all instances of that value in-place ...
- 27. Remove Element@python
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 【LeetCode】27. Remove Element (2 solutions)
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- (双指针) leetcode 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
随机推荐
- nginx中实现把所有http的请求都重定向到https
在网站启用https之后,我们可能会有一个需求,就是将所有的http的请求自动地重定向到https, 如果前端是使用的nginx来实现的https,我们可以这样配置nginx的301重定向: serv ...
- Dockerfile的一些demo
tomcat7.0_jdk1.6 #继承用户创建的sshd镜像FROM yatho:sshd_ubuntu #创建者的基本信息 MAINTAINER yatho (yat_ho@163.com) #设 ...
- C# 读取xml时,遇到xmlns的问题
1.读取xml的时候,由于xml里有xmlns的属性,导致了读xml无法正常读取.通过网上搜索,发现需要先注册命名空间. xmlns是XML Namespaces的缩写,中文名称是XML(标准通用标 ...
- 使用sql语句删除数据库中重复的数据
在数据库中有一批数据,但是由于数据入库之前某些不当的操作引入了一些重复的数据,需要将这些重复的数据删除.数据的结构大概是下面的样子 CREATE TABLE [dbo].[aaa]( [id] [in ...
- Instrumentation 两种方法 premain Agent
由于jvm内部的限制Instrumentation 只能修改方法体 不能动态添加删除方法(安全第一吧!!!!) Premain 对于使用命令行接口的实现,可以将以下选项添加到命令行来启动代理: -ja ...
- 吐槽win8
想吐槽win8好久了!今天终于忍不住,得喷一下以泻心头之愤. 文件拷贝动辄失败!? 但凡是我从远程往本地拷大程序,此时文件系统变得奇慢无比,而且本地也不能打开大文件或者往外拷贝,否则一不小心就出现拷贝 ...
- CSS基础问题
1.css引入问题 本来以为css引入是很简单的问题,但是在写demo中,使用连接方式引入就出现了问题,找了半天,还是说一下问题吧. 在引入时没有写rel="stylesheet" ...
- mysqladmin 命令详解
mysqladmin是一个执行管理操作的客户端程序.它可以用来检查服务器的配置和当前状态.创建和删除数据库等. mysqladmin 工具的使用格式: mysqladmin [option] comm ...
- FZU 2252 Yu-Gi-Oh!(枚举+贪心)
Problem 2252 Yu-Gi-Oh! Accept: 105 Submit: 628 Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- JSP学习_02
JavaBean是特殊的Java类,使用Java语言编写,遵守JavaBean规范JavaBean同其他Java类一些独一无二的属性:拥有一个默认的构造函数需要被序列化并实现Serializable接 ...