(Array)27. Remove Element
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的更多相关文章
- leetcode解题报告(8):Remove Element
描述 Given an array and a value, remove all instances of that value in place and return the new length ...
- 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 ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- C# 中的数组(array)
原文 C# 中的数组(array) 特性 数组是一个无序的元素序列.数组元素存储在一个连续性的内存块中,并可使用一个整数索引来访问. C# 声明数组变量时,数组的大小不是声明的一部分.这点与C/C++ ...
- R语言编程艺术# 矩阵(matrix)和数组(array)
矩阵(matrix)是一种特殊的向量,包含两个附加的属性:行数和列数.所以矩阵也是和向量一样,有模式(数据类型)的概念.(但反过来,向量却不能看作是只有一列或一行的矩阵. 数组(array)是R里更一 ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)
目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- GoLang基础数据类型--->数组(array)详解
GoLang基础数据类型--->数组(array)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Golang数组简介 数组是Go语言编程中最常用的数据结构之一.顾名 ...
随机推荐
- [转载]javaEE规范和SSH三大框架到底有什么关系
转载自: http://blog.csdn.net/bingjing12345/article/details/20641891 1994-2000 年是互联网的大航海时代. 请注意,下面的时间点及其 ...
- HDU 3397 Sequence operation
题目:下列操作 Change operations:0 a b change all characters into '0's in [a , b]1 a b change all character ...
- Node.js 事件
Node.js 事件 Node.js 所有的异步I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, ...
- JVM-并发-Java 内存模型
Java内存模型 (1). 主内存与工作内存 Java内存模型规定了所有的变量都存储在主内存中. 每类线程的变量的主内存副本拷贝,线程对变量的所有操作(读操作,赋值操作等)都必须工作内存中进行,而不能 ...
- springmvc 向页面传值
- disconnected no supported authentication methods available(server sent: publickey,keyboard interae)
因为乌龟Git和Git的冲突 我们需要把乌龟Git设置改正如下. 找到TortoiseGit -> Settings -> Network 将SSH client指向~\Git\bin\s ...
- video 与 audio
Play方法:使用play方法来播放媒体,自动将元素的paused属性的值变为false.Pause方法:使用pause方法来暂停播放,自动将元素的paused属性的值变为true.Load方法:使用 ...
- IIS-反向代理
测试环境:Windows10.IIS/10.0 1.安装ARR.URL Rewrite(URL重写工具2.0) 注意英文和中文环境的对应: Application Request Routing 对应 ...
- ArcGIS+API+for+JS测距
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PHP DES 加解密
代码很简单,如下: <?php $key = 'very important data'; function jiami($key, $str) { /* Open module, and cr ...