(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语言编程中最常用的数据结构之一.顾名 ...
随机推荐
- ASP.NET中使用代码来进行备份和还原数据库
ASP.NET中使用代码来进行备份和还原数据库 SQL代码: 1 2 3 4 5 -- 备份数据库 backup database db_CSManage to disk='c:\backup.ba ...
- juqery easyui
私人做程序开发一直有个头疼的问题就是后台管理界面,以前一般都是自己用jquery+ps自己设计的,效果很一般,很不理想. 今天初次使用Jquery EasyUi,简单的做了个布局页面感觉还不错,给大家 ...
- NSRunLoop概述和原理
1.什么是NSRunLoop?我们会经常看到这样的代码: - (IBAction)start:(id)sender{pageStillLoading = YES;[NSThread detachNew ...
- 必备技能:分清楚DOM的attribute和property
分清楚DOM的attribute和property,用JQ的时候分清楚attr,和prop方法,网上有很多大神的总结,我就不列举了.
- Create Linked Server SQL Server 2008
From:http://www.jensbits.com/2010/11/10/create-linked-server-sql-server-2008/ http://www.c-sharpcorn ...
- 百度定位并获取县区天气-XML+fragment+sqlite
此工程较BaiduLocationXMLFragment相比:1.加入数据库部分,将获取到的地址 天气存入数据库中,离线状态显示数据库最后一条记录 sqlite: DatabaseHelper.ja ...
- uoot启动过程
1.从我们的start_armboot开始讲起 u-boot整体由汇编段和C语言段外加连接脚本组成.关于汇编段请看我之前的博客<u-boot源码汇编段简要分析>,好,让我们进入start_ ...
- CSS第一天总结
CSS是层叠样式表,其作用在我看来就是统一一个或多个元素或者ID.class等的属性,CSS可以定义的属性非常多,一个好看的网页离不开CSS的修饰. CSS简而言之就是三个部分:选择符.属性.属性值. ...
- Matcher.group
Exception in thread "main" java.lang.IllegalStateException: No match found at java.util.re ...
- 浅谈对ECharts的使用
上个月的项目,其中有一个模块用的是ECharts来实现的,分别用了折线图,环形图,还有漏斗图,这几个都算比较常见的了,尤其是折线图,环形图,用的最多的就是它们了.之前也没怎么接触过ECharts,实际 ...