27. Remove Element(双指针)
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 ofnums
containing0
,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(双指针)的更多相关文章
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- 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 ...
- 27. Remove Element@python
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 【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 ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- (双指针) leetcode 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
随机推荐
- rc.sysinit 解析
$# :它可抓出 positional parameter 的數量,即脚本后面的参数有几个 $@和$*表示全部参数,但不包含脚本名,即$0,如果在command line上跑 my.sh p1 “p2 ...
- sphinx配置文件详解
sphinx的配置文件是在配置的时候最容易出错的了: 我们先要明白几个概念: source:数据源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一 ...
- JavaScript—倒计时
当前时间-倒计时下载 效果: 代码: <!doctype html> <html> <head> <meta http-equiv="Content ...
- 【CF914G】Sum the Fibonacci 快速??变换模板
[CF914G]Sum the Fibonacci 题解:给你一个长度为n的数组s.定义五元组(a,b,c,d,e)是合法的当且仅当: 1. $1\le a,b,c,d,e\le n$2. $(s_a ...
- AFNetWork 简单实用demo
NSString *postUrl = @"http://www.huway.com/api_index?module=event&action=topads"; NSDi ...
- Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等
extends:http://blog.csdn.net/h7870181/article/details/12505883 Google Maps API 网络服务 官网地址 : https://d ...
- Unity3D 面试ABC
最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...
- 利用bat批处理做启动mongodb脚本
文章开始,我们先回顾一下,如何用cmd命令窗口开启mongodb数据库,命令如下: 开启mongodb数据库 cd D:\Program Files\MongoDB\bin mongod --depa ...
- 【转】bit、byte、位、字节、汉字的关系
UTF-8和UTF-16的区别?Unicode和UTF是什么关系?Unicode转义字符(\u+4个十六进制).遇到多个引号的时候转义? 解释: unicode是一种编码方式,和ascii是同一个概念 ...
- oracle的日期相减
oracle的日期相减 : 两个date类型的 日期相减,得到的是天数,可能是带小数点的.如下: