题目链接:https://leetcode.com/problems/remove-element/description/

题目大意:给出一个数组和一个值,从数组中删除与当前值相等的值,并将数组长度返回,最终数组中元素的顺序可以不保持原顺序,也就是允许排序。

法一:反向思考,不直接删除值,而是将不等于当前值的其他数保留在数组中,用一个下标值重新记录数组下标。代码如下(耗时11ms):

     public int removeElement(int[] nums, int val) {
int k = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != val) {
nums[k++] = nums[i];
}
}
return k;
}

法二:正向思考,直接删除值,也就是每删除一个值,就记录一下个数,然后在将不等于当前值的数留在数组中时,更新下标是i-k。代码如下(耗时9ms):

     public int removeElement(int[] nums, int val) {
int k = 0, i = 0;
for( ; i < nums.length; i++) {
if(nums[i] == val) {
k++;
}
else {
nums[i - k] = nums[i];
}
}
return i - k;
}

27.Remove Element---两指针的更多相关文章

  1. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  2. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  3. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  4. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  5. C# 写 LeetCode easy #27 Remove Element

    27. Remove Element Given an array nums and a value val, remove all instances of that value in-place  ...

  6. 【LeetCode】27. Remove Element (2 solutions)

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

  7. 27. Remove Element@python

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

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

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

  9. (Array)27. Remove Element

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

  10. 27. Remove Element[E]移除元素

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

随机推荐

  1. [CodeForces948B]Primal Sport(数论)

    Description 题目链接 Solution 设f(x)为x的最大质因子 那么由题意易得\(X_1\)的范围在\([X_2-f(X_2)+1,X2]\) 同理\(X_0\)的范围在\([X_1- ...

  2. gcc常用命令

    1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...

  3. linux上Kettle定时执行(转换的单步执行,job的单步执行,环境变量,kettle定时功能,效率问题等)转自(http://blog.csdn.net/feng19821209/article/details/5800960)

    1,Kettle跨平台使用.    例如:在AIX下(AIX是IBM商用UNIX操作系统,此处在LINUX/UNIX同样适用),运行Kettle的相关步骤如下:    1)进入到Kettle部署的路径 ...

  4. android 事件拦截 (Viewpager不可以左右滑动)

    以前没有做过真正的需求,所以从来没有觉得事件拦截分发处理有什么好懂的. 现在做需求了,真的是什么需求都有,你作为开发都要去研究实现.比如说,只能点不能滑动的viewpager.其实这都可以不用view ...

  5. webstorm git提交不成功的

    git pull git pull origin master git pull origin master --allow-unrelated-histories

  6. Python基础——安装运行

    Python是如何运行的? 像绝大多数编程语言一样,要在计算机上能够运行python程序,至少需要安装一个最小的Python包:一个Python解释器和支持的库. 安装Python 安装包下载:htt ...

  7. 图解java面试

    图解Java面试题:基本语法 2017-02-07 14:34 出处:清屏网 人气:178 评论(0)   内容大纲.png &和&&的区别 &和&&的 ...

  8. Win10开始菜单中的天气不更新问题的解决方法

    两台电脑同时做的Win10系统,最新的1703 Creator Update 版本,其中一台的开始菜单中天气方块总是显示图标,试了各种方法都不行,最后是点开天气App,在App的顶端有几个按钮,其中有 ...

  9. 剑指Offer - 九度1391 - 顺时针打印矩阵

    剑指Offer - 九度1391 - 顺时针打印矩阵2013-11-24 04:55 题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 ...

  10. leetcode 【Rotate List 】python 实现

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...