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. c# 配置文件之configSections配置(二)

    在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的 System.Configuration.DictionarySectionHandler System. ...

  2. 编程技术●Python

    <Python语言入门> 2015-01-16 14:13 ★ 虽然书名叫入门.序里也写了说完全没有编程经验的都可以用这本书来学习入门.不过好像不太适合哦.书很好,内容也挺全面细致的.太好 ...

  3. Android开源框架:Universal-Image-Loader解析(一)

    之前花了一些时间,好好看了下这个框架,于是决定再重新梳理一下,把整个处理方法和流程过一遍,俗话说:温故而知新嘛 关于Universal-Image-Loader此框架的各种优点,稍微介绍下,网上应该也 ...

  4. 《C++primer》v5 第3章 字符串、向量和数组 读书笔记 习题答案

    本章问题 1.char *p="hello world";与char p[]="hello world"的问题. 简单说前者是一个指向字符串常量的指针,后者是一 ...

  5. ubuntu 14.04 安装docker

    常会遇到的问题就是网络的问题,如访问https://get.docker.io/ 遇到403的问题:总结一下最简单的几条命令: $ sudo apt-get install apt-transport ...

  6. 2016 - 1 - 23 json转模型 常用的第三方框架

    一: 三个常用的框架 1. Mantle - 所有模型必须继承MTModel 2. JSONModel - 所有模型必须继承JSONModel 3.MJExtension - 不需要继承任何东西. - ...

  7. C语言数组初始化

    例如: int a[15] = {0}; 第一种,编译器会把第一个初始化值赋给数组的第一个元素,然后用0赋给其余的元素.如果没有给出初始值,编译器不会去做初始化工作.这样简洁的方式让代码更加高效. 还 ...

  8. springmvc工作原理以及源码分析(基于spring3.1.0)

    springmvc是一个基于spring的web框架.本篇文章对它的工作原理以及源码进行深入分析. 一.springmvc请求处理流程 二.springmvc的工作机制 三.springmvc核心源码 ...

  9. Erlang 103 Erlang分布式编程

    Outline 笔记系列 Erlang环境和顺序编程Erlang并发编程Erlang分布式编程YawsErlang/OTP 日期              变更说明 2014-11-23 A Outl ...

  10. iOS常用设计模式和机制之Block简单使用

    Block :block 实际上就是 Objective-C语言对闭包的实现 闭包(Closure):闭包就是一个函数,或者一个指向函数的指针,加上这个函数执行的非局部变量.闭包允许一个函数访问声明该 ...