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 of nums containing 0, 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(双指针)的更多相关文章

  1. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  2. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  3. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  4. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  5. 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  ...

  6. 27. Remove Element@python

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  7. 【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 ...

  8. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  9. (双指针) leetcode 27. Remove Element

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

随机推荐

  1. dig命令安装

    yum -y install bind-utils  Dig是一个在类Unix命令行模式下查询DNS包括NS记录,A记录,MX记录等相关信息的工具 查找yahoo.com的A记录:(此处一定是域而不是 ...

  2. calloc(), malloc(), realloc(), free(),alloca()

    内存区域可以分为栈.堆.静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取的方式都是由编译器自动执行的. 利用指针,我们可以像汇编语言一样处理内存地址,C 标准函数库提 ...

  3. rpmdb: unable to join the environment的问题解决

    今天笔者在Centos 6.3上使用yum安装lsof软件时,报如下错误: [root@ ~]# yum install lsof -y rpmdb: unable to join the envir ...

  4. [sharepoint]Office Web Apps for SharePoint 2010

    Office Web Apps for SharePoint 2010 2012年09月20日 ⁄ 综合 ⁄ 共 908字 ⁄ 字号 小 中 大 ⁄ 评论关闭 After you install Of ...

  5. wordpress---page页面数据调用

    在wordpress的开发中,会使用wordpress的的页面,那么页面数据该怎么调用呢? 拿到页面的 content: <?php if(have_posts()) : ?> <? ...

  6. thinkCMF----使用自定义函数

    thinkCMF使用自定义函数:app 下新建 common.php

  7. 关于Thinkphp访问不正常的问题

    最近遇见了个蹩脚的问题,我放在服务器的项目(thinkphp框架)只能访问默认路径内容,不管你url怎么写的,他就访问默认那个文件.. 对于有强迫症的我来说实在是欺人太甚!!! 于是乎我就抓耳挠腮了. ...

  8. Saltstack生产案例之Haproxy安装

    cd /srv/salt/prod/ mkdir haproxymkdir keepalivedmkdir nginxmkdir phpmkdir memcachedmkdir pkg cd pkg ...

  9. 《into100-创客+沙龙第4期:互联网产品用户需求挖掘与转化》圆满成功

    5月16日,由麦思博和阿里云联合主办的<into100-创客+沙龙第4期:互联网产品用户需求挖掘与转化>圆满结束.现场100多位来自互联网及软件公司的产品经理.产品总监.开发经理.工程师及 ...

  10. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...