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, ...
随机推荐
- Codeforces Round #362 (Div. 2)
闲来无事一套CF啊,我觉得这几个题还是有套路的,但是很明显,这个题并不难 A. Pineapple Incident time limit per test 1 second memory limit ...
- hdu 4251 The Famous ICPC Team Again划分树入门题
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 03-offsetParent属性
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 【Luogu】P3856公共子串(DP)
题目链接 DP.设last[i][j]是第i个串字符'j'所在的最后的位置,f[i][j][k]是第一个串匹配到i,第二个串匹配到j,第三个串匹配到k,最多的公共子串数. 那么我们三重循环i.j.k, ...
- DP的序--Codeforces626F. Group Projects
$n \leq 200$个数,$ \leq 500$,$K \leq 1000$代价内的数字分组有多少?一个分组的代价是分成的每个小组的总代价:一个小组的代价是极差. 问的极差那就从极入手嘛.一个小组 ...
- css-包含块
在CSS中,有事一个元素的位置和尺寸的计算都相对于一个矩形,这个矩形被称作包含块.包含块是一个相对的概念,比如 子元素的初始化布局总是在父元素的左上角,这就是一个相对的概念.其中父元素就是一个参照物, ...
- android 禁止ViewPager滑动
最近项目中,有个需求就是要禁止ViewPager滑动事件,我们看下360手机助手的界面,风格就类似这样的 大家如果使用过360手机助手就会发现中间内容是不可以滑动的,现在写一个demo,讲下怎么禁止V ...
- 洛谷—— P1605 迷宫
P1605 迷宫 题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在 ...
- #ifdef #endif #if #endif
c语言里所有以#开头的都是预编译指令,就是在正式编译之前,让编译器做一些预处理的工作. #ifdef DEBUG printf("variable x has value = %d\n&qu ...
- eclipse软件安装及python工程建立
原文地址:http://www.cnblogs.com/halfacre/archive/2012/07/22/2603848.html 安装python解释器 安装PyDev: 首先需要去Eclip ...