LeetCode(27)题解:Remove Element
https://leetcode.com/problems/remove-element/
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
思路:
因为可以改变元素顺序,所以只需要把最后端的非val元素用最前端的val代替即可,前后两个指针同时移动直到相遇。
AC代码:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i=,n=nums.size()-;
if(n<)
return ;
while(nums[n]==val)
n--;
if(n<)
return ;
while(i<n){
while(nums[n]==val){
n--;
}
if(i>n)
break;
if(nums[i]==val){
nums[i]=nums[n];
n--;
}
i++;
}
while(nums[n]==val){
n--;
}
return n+;
}
};
LeetCode(27)题解:Remove Element的更多相关文章
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- LeetCode(27)Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- 【算法】LeetCode算法题-Remove Element
这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...
- 【LeetCode】027. Remove Element
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- LeetCode(83)题解: Remove Duplicates from Sorted List
https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, de ...
- 【LeetCode OJ】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length ...
- LeetCode OJ:Remove Element(移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- LeetCode(82)题解: Remove Duplicates from Sorted List II
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目: Given a sorted linked list, ...
随机推荐
- matlab中函数学习——11月14日
1.记录数组元素个数函数:numel() 解释:number of array 相当于 prod(size(A)) 2.添加路径: addpath('.\3rdparty\ksvd'); 3.pada ...
- 【Luogu】P1602Sramoc问题(堆)
题目链接 很巧妙的想法.一开始将1~k-1加入堆中,然后每次从堆里取出一个最小的,判断是不是答案,如果不是,那么就枚举新数的末一位加上. 代码如下 #include<cstdio> #in ...
- Jerasure库接口简介及性能测试
http://blog.chinaunix.net/uid-20196318-id-3277600.html Jerasure库提供Reed-Solomon和Cauchy Reed-Solomon两种 ...
- .net面试题汇总-第二篇
本篇主要关注下,.net面试题中经常用的算法问题 1.有一群猴子,它们每天要吃桃子,它们第一天吃的数量是总量的一半再多一个,第二天吃的是第一天剩下的一半再多一个,第三天吃的是第二天剩下的一半多一个,以 ...
- MongoDB_起步
MongoDB基本概念 <1> mogoDB是一个文档存储类型的nosql数据库,文档存储一般用类似json的格式存储,存储的内容是文档型的. 这样也就有机会对某些字段建立索引, < ...
- noj 2069 赵信的往事 [yy题 无限gcd]
njczy2010 2069 Accepted 31MS 224K 1351Byte G++ 2014-11-13 13:32:56.0 坑爹的无限gcd,,,尼玛想好久,原来要x对y算一次,y再 ...
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
- R语言入门视频笔记--5--自定义函数
自定义函数 你可以输出一段代码,创建一个你自己定义的函数 蛋是如果你两个自定义函数的名字重复的话,后面的会把前面的替换掉 举个栗子: hanshu1 <- function(x) sqrt(v ...
- centos6安装概述
1.1.选择安装类型:[Install or upgrade an existing system]安装或升级现有系统 1.2.介质校验:[Skip]跳过介质校验,校验时间较长 1.3.语言选择:[E ...
- InteliJ 安装PlantUML插件
打开InteliJ点击Setting 在[Plugins]搜索PlantUML插件,点击绿色的Install安装 然后重启 完成