Given an array and a value, 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 in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

public class Solution {
public int removeElement(int[] nums, int val) { //更好的方法是设置两个指针都是从0开始,这样直接return i了
int i = 0;
int j = nums.length - 1;
while(j > i) {
while (nums[i] != val && j>i) //注意这个条件
i++;
while (nums[j] == val && j>i)
j--;
swap(nums, i++, j--);
}
int res = 0;
for (i = 0; i < nums.length; i++) {
//System.out.print(nums[i] + " ");
if (nums[i] != val)
res++;
}
return res; } public void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

  

(Array)27. Remove Element的更多相关文章

  1. leetcode解题报告(8):Remove Element

    描述 Given an array and a value, remove all instances of that value in place and return the new length ...

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

  3. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  4. C# 中的数组(array)

    原文 C# 中的数组(array) 特性 数组是一个无序的元素序列.数组元素存储在一个连续性的内存块中,并可使用一个整数索引来访问. C# 声明数组变量时,数组的大小不是声明的一部分.这点与C/C++ ...

  5. R语言编程艺术# 矩阵(matrix)和数组(array)

    矩阵(matrix)是一种特殊的向量,包含两个附加的属性:行数和列数.所以矩阵也是和向量一样,有模式(数据类型)的概念.(但反过来,向量却不能看作是只有一列或一行的矩阵. 数组(array)是R里更一 ...

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

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

  7. 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)

    目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...

  8. 27. Remove Element【leetcode】

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

  9. GoLang基础数据类型--->数组(array)详解

    GoLang基础数据类型--->数组(array)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Golang数组简介 数组是Go语言编程中最常用的数据结构之一.顾名 ...

随机推荐

  1. 4、时间同步ntp服务的安装于配置(作为客户端的配置)

    yum安装ntpd服务   .yum -y install ntp ntpdate (安装时间同步ntp服务) . vi /etc/ntp.conf (修改ntpd服务的配置文件)   3.修改配置文 ...

  2. 如何修改WAMP中mysql默认空密码 以及修改时报错的处理方法

    WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...

  3. UIkit框架之UIDatePicker

    1.继承链:UIcontrol:UIview:UIResponder:NSOobject 2.和uidatepicker相关联的触发事件是 UIControlEventValueChanged,当使用 ...

  4. Boundaries

    Using Third-Party Code There is a natural tension between the provider of an interface and the user ...

  5. linux内核分析——扒开系统调用的三层皮

    万子惠 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验部分 选择2 ...

  6. Pylot压力测试(linux)

    Pylot需要python2.5以上的版本,打开以后选择对应你的系统的版本,下载好之后双击安装. centOS5.5 系统版本python版本是2.4.3,所以要下载个2.5以上的. 1.下载Pyth ...

  7. notifyDataSetInvalidated()跟notifyDataSetChanged()的区别

    public void notifyDataSetChanged(): 通过一个外部的方法控制,如果适配器的内容改变了,那么就会强制调用getView来刷新每个Item的内容.这个方法内部实现了在每个 ...

  8. Java-->xml的pull解析

    --> pull解析器是android内置的解析器,解析原理与sax类似 --> xml文件student.xml: <?xml version="1.0" en ...

  9. LintCode Count 1 in Binary

    知识点 1. 整数的二进制表示法 2. 十进制和二进制的转换 http://baike.baidu.com/view/1426817.htm 3. 负整数的表示(原码,补码,反码) http://ww ...

  10. 如何在JBoss WildFly 8 自定义log4j日志

    最近在 JBoss WildFly 8 下部署 Web应用,自定义的 log4j 日志不工作.console下无日志输出,用System.out.println都不输出内容到console. 原因是J ...