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的更多相关文章

  1. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  2. LeetCode(27)Remove Element

    题目 Given an array and a value, remove all instances of that value in place and return the new length ...

  3. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  4. 【算法】LeetCode算法题-Remove Element

    这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...

  5. 【LeetCode】027. Remove Element

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  6. LeetCode(83)题解: Remove Duplicates from Sorted List

    https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, de ...

  7. 【LeetCode OJ】Remove Element

    题目:Given an array and a value, remove all instances of that value in place and return the new length ...

  8. LeetCode OJ:Remove Element(移除元素)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  9. LeetCode(82)题解: Remove Duplicates from Sorted List II

    https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目: Given a sorted linked list, ...

随机推荐

  1. Codeforces Round #204 (Div. 2)

    D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. php增加

    <?php$lname = $_POST['lname'];$name = $_POST['name'];$number = $_POST['number'];$dod = $_POST['do ...

  3. hdu2081

    #include <stdio.h> #include <malloc.h> int main(){ ]; char *p; int t; p=(); scanf(" ...

  4. Selenium WebDriver高级用法

    Selenium GitHub地址 选择合适的WebDrvier WebDriver是一个接口,它有几种实现,分别是HtmlUnitDrvier.FirefoxDriver.InternetExplo ...

  5. iOS中的过滤器和正则表达式(NSPredicate,NSRegularExpression)

    参考链接:http://www.cocoachina.com/industry/20140321/8024.html NSPredicate Cocoa提供了一个NSPredicate类,它用来指定过 ...

  6. Luogu【P1901】发射站(单调栈)

    题目链接 题目说明比自己矮的塔收不到自己的能量,摆明了就是单调栈呗. 把比自己矮的全都从栈里弹出去,于是碰到第一个比自己高的.让他接受自己发射的能量. 当然由于发射站发射的能量有两个方向,所以正反两遍 ...

  7. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  8. 【2018.11.8】小迟的比赛 / Yuno like cake / 格子填数

    题目 $noip$ 欢乐赛真是欢乐,除了不欢乐的方面以外我都很欢乐. T1 鸡汤题目,故意输对后面的胜率又没有影响,为什么要故意输呢? 所以第二个决策是凑字用的,这题就是朴素递推概率,最后乘结果权值计 ...

  9. cf701E Connecting Universities

    Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's poss ...

  10. Nk 1214 Relatives(欧拉函数)

    Time Limit: 1500 ms    Memory Limit: 10000 kB   Total Submit : 234 (77 users)   Accepted Submit : 10 ...