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. 【LeetCode】To Lower Case(转换成小写字母)

    这道题是LeetCode里的第709道题. 题目要求: 实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串. 示例 1: ...

  2. ubuntu14.04LTS root登录出现错误

    输入root帐号和密码,出现以下错误: Error found when loading /root/.profile stdin:is not a tty 解决方法: 在终端中用命令:gedit / ...

  3. hdu2087

    #include <stdio.h> #include <string.h> int main(){ int cnt,i,j,k; +],tmp[+]; int strl,tm ...

  4. LCA+主席树 (求树上路径点权第k大)

      SPOJ 10628. Count on a tree (树上第k大,LCA+主席树) 10628. Count on a tree Problem code: COT You are given ...

  5. git 本地保存账号密码

    用ssh连接的项目都不用输账号密码 如果https的话   每次都用输入账号密码   很繁琐 解决方法,在本地的工程文件夹的.git下打开config文件添加: [credential]     he ...

  6. 【Luogu】P1013进制位(搜索)

    题目链接在这里 这题和虫食算比较类似.做完这道题可以去做虫食算.都是搜索一类的题. 这样 我们分析题目可以发现进制只可能是字母的个数,也就是n-1.为什么? 因为题目要求完整的加法表才算数.如果进制低 ...

  7. 高通android7.0刷机工具使用介绍

    刷机工具安装 1. 安装QPST.WIN.2.7 Installer-00448.3 2. 安装python2.7,并配置其环境变量 刷机方法 1.将编译后的刷机文件拷贝到如下目录:SC20_CE_p ...

  8. *Codeforces587E. Duff as a Queen

    $n \leq 200000$的序列,支持以下$q \leq 4e4$个操作:区间异或$k$:查询区间能异或出多少不同的数.数字$0 \leq a_i \leq 1e9$. 大概是要区间线性基.区间修 ...

  9. linux 查看自己所在的公网ip

    curl members.3322.org/dyndns/getip 其他的方法还有: curl icanhazip.com curl ifconfig.me curl curlmyip.com cu ...

  10. msp430项目编程50

    msp430综合项目---gsm无线采集传输平台系统50 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结