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 ...
随机推荐
- kvm虚拟机不能使用virsh shutdownw命令关闭虚拟机的解决方法
今天笔者在对kvm虚拟机进行管理时,使用virsh shutdown命令关闭指定的虚拟机时,发现虽然有如下的提示,但其实虚拟机却一直不会真正的关闭. 经过查看virsh命令帮助和上网查询,才得知vir ...
- 【CF888E】Maximum Subsequence 折半搜索
[CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...
- Windows 8.1 100% 磁盘使用率解决方案
前段时间我的win8电脑爆卡!动不动就卡死,一点都动不了. 好不容易打开了任务管理器,发现disk usage: 100%,实在是不理解,磁盘使用率100%怎么会影响流畅度?如果是CPU或内存还好理解 ...
- iOS - 获取音视频文件的Metadata信息
// // MusicInfoArray.h // LocationMusic // // Created by Wengrp on 2017/6/22. // Copyright © 2017年 W ...
- 【转载】51单片机data,bdata,idata,xdata使用注意事项
"51单片机编程在不同内存空间data xdata bdata定义变量的注意事项": 关键词:51 单片机 编程 不同 内存空间 data xdatabdata 定义 变量 注意事 ...
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- T49
明天参加媳妇朋友的婚礼.今天晚上的火车,下班后匆忙的打了个的,正好到的哥交接班的时间拦了几辆车都不拉火车站!无奈-五分钟后打上车接上媳妇去火车站!正值五中学生放假路上各种堵!安阳这四线城市什么时候变的 ...
- ELK之nginx日志使用json格式输出
json Nginx默认日志输出格式为文本非json格式,修改配置文件即可输出json格式便于收集以及绘图 修改nginx配置文件添加配置,增加一个json输出格式的日志格式 log_format a ...
- KMP算法小记
Knuth-Morris-Pratt算法: 转载来自http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_ ...
- 一句替换bbcode
$message=preg_replace('/\[[^\[\]]{1,}\]/','',$message);