1 题目

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.

Hide Tags

Array Two Pointers

 
2 思路
好吧,感觉这个题目出的不是很好,按照题目要求,要改变A数组的长度,我一想,数组长度怎么改。。,看了答案,原来只是把和elem相同的元素,取另外一个元素覆盖这个相同的元素即可。也就是结果应该是前面startPosition(最终返回的结果)个元素里面没有elem,且是A[]数组里面排出elem剩下的元素。
 
3 代码
    public int removeElement(int[] A, int elem) {
int startPosition = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] != elem) {
A[startPosition++] = A[i];
}
}
return startPosition;
}

[leetcode 50]remove element的更多相关文章

  1. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  2. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  3. LeetCode 027 Remove Element

    题目要求:Remove Element Given an array and a value, remove all instances of that value in place and retu ...

  4. LeetCode 27. Remove Element (移除元素)

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

  5. [LeetCode] 27. Remove Element 移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  6. LeetCode 27 Remove Element

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

  7. 【leetcode】Remove Element

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

  8. 【leetcode】Remove Element (easy)

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

  9. Leetcode 27 Remove Element STL

    和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...

随机推荐

  1. tar 解压某个指定的文件或者文件夹

    1. 先查看压缩文档中有那些文件,如果都不清楚文件内容,然后就直接解压,这个是不可能的 使用#tar -tf 压缩包名称,可以查看压缩包内容 2.解压某个文件 tar -zxvf zabbix.tar ...

  2. android xml解析中的null问题

    当我们从服务器或者xml文件加载xml进行解析的时候,往往报告 nullpointer 错误.这是原始代码: String short_name = doc.getElementsByTagName( ...

  3. [Jmeter] Jmeter Plugins

    Plugins: Plugins Manager: https://jmeter-plugins.org/wiki/PluginsManager/ Custom Thread Groups: http ...

  4. Emacs, Nano, or Vim 编辑器“三剑客”

    # Vim 强大,多种模式相互切换,不同于传统“录入式“写东西 对后续两个编辑器不熟悉 # nano 退出ctrl + x http://man.linuxde.net/nano # emacs ht ...

  5. Windows 平台 (UWP)应用设计

    Make Your Apps Cooperate with Cross-App Communication :  https://rewards.msdn.microsoft.com/Challeng ...

  6. NOIP训练测试2(2017081502)

    唔,这是今天第二场训练测试. 上一轮不够难,现在来一波更简单的.[滑稽] 注意时间! 测试时间:3小时 题目一:Cantor表 题目二:回文数 题目三:拼数 题目四:进制位 题目五:邮票面值设计 都是 ...

  7. HDU - 5658

    题意:给你一个字符串,给你Q次询问,每一次问你从l-r里有多少个回文串. 思路:len很小,所以直接遍历区间求就好了. /* gyt Live up to every day */ #include& ...

  8. Python之字符串基本操作

    #!/usr/bin/env python#-*-coding utf8-*-#Author:caojininfo = { 'stu1001': 'caojin', 'stu1002': 'zhaom ...

  9. 再读c++primer plus 002

    1.读取char值时,与读取其它基本类型一样,cin将忽略空格和换行符,函数cin.get(ch)读取输入的下一个字符(即使是空格),并将其赋给变量ch. 2.指针和const:(1)让指针指向一个常 ...

  10. mysql安装后初始密码

    在安装过程中没有任何提示,安装完之后无法登陆 后经查询发现,可以暂时以 mysql -u root -p登陆 此账户没有密码直接enter即可. update user set Password=PA ...