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 ofnums
containing0
,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 ...
随机推荐
- POJ 1117 Pairs of Integers
Pairs of Integers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4133 Accepted: 1062 Des ...
- ELK系列三:Elasticsearch的简单使用和配置文件简介
1.定义模板创建索引: 首先定义好一个模板的例子 { "order":14, "template":"ids-1", "state ...
- Linux(CentOS)安装JDK(.tar.gz)并配置
本文思路转自http://blog.sina.com.cn/s/blog_81631744010137iy.html 点击(此处)折叠或打开 1.到 甲骨文(oracle)下载jdk不用多说 tar ...
- [转][darkbaby]任天堂传——失落的泰坦王朝(中)
TV游戏产业历史上曾有过太多表里不一的外交辞令,然而当年SQUARE和任天堂分道扬镳的真正原因确实如坂口博信在1996年2月29日的PS版 <FFVII>发表会上宣称的那样:“虽然之前有过 ...
- you do not have permission to pull from the repository解决方法
使用git进行项目的版本管理,换了台电脑,配置了账号和邮箱后,pull一个私有项目的时候,发现一个问题: 原因分析: 这是由于没有设置Gitee的SSH公钥.在未设置SSH公钥的情况下,可以使用git ...
- thinkphp结合layui上传图片
简单示例: <script type="text/javascript"> layui.use(['form', 'layedit','element', 'layda ...
- 安装coreseek与sphinx遇见的问题
1.问题 using config file 'etc/csft.conf'...indexing index 'xml'...WARNING: source 'xml': xmlpipe2 supp ...
- Oracle之RMAN备份恢复1-基础篇
1,rman的作用与体系架构 1.1 什么是rman rman(recovery manager)是oracle8i以后dba的一重要工具一般位于$oracle_home/bin目录下,主要用来备份, ...
- URL长度过长的问题
最近项目中很多跨域的问题,有时候跨域要传递很多参数,甚至有时候要传递整个对象,处理的方法是把对象转换成JSON形式的字符串再传递.此时该JSON字符串就比较长,作为参数附加到URL后面,URL就会变得 ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...