题目链接: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. Android 程序中获取一个反向 Shell

    代码如下: String command = "ls -al /"; Process process = Runtime.getRuntime().exec(command); 之 ...

  2. Spark 源码阅读——任务提交过程

    当我们在使用spark编写mr作业是,最后都要涉及到调用reduce,foreach或者是count这类action来触发作业的提交,所以,当我们查看这些方法的源码时,发现底层都调用了SparkCon ...

  3. Oozie 实战之 shell

    说明:使用 shell action 执行 shell 脚本 hive-select-test.sh 来通过已经配置好的 Hive -f 来执行 HQL 查询脚本文件 select.sql 1.创建脚 ...

  4. PHP.36-TP框架商城应用实例-后台12-商品管理-主分类添加、修改、搜索(连表查询)

    需求:一个商品必须有一个主分类,一个主分类可以有多个商品 [一对多] 修改表p39_goods,增加外键约束,增加索引 主分类添加[控制器->页面] 1.在控制器GoodsController. ...

  5. The Image Gallery Revisited

    Lets apply the Best practices to the Image Gallery . /*** index.html ***/ <!DOCTYPE html> < ...

  6. 微信支付 h5

    Android开发要点说明 商户在微信开放平台申请开发应用后,微信开放平台会生成APP的唯一标识APPID.由于需要保证支付安全,需要在开放平台绑定商户应用包名和应用签名,设置好后才能正常发起支付. ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 【Kernel Logistic Regression】林轩田机器学习技术

    最近求职真慌,一方面要看机器学习,一方面还刷代码.还是静下心继续看看课程,因为觉得实在讲的太好了.能求啥样搬砖工作就随缘吧. 这节课的核心就在如何把kernel trick到logistic regr ...

  9. PC Server远程管理卡用户管理脚本实现

    1.IPMI工作原理图: 2.脚本实现流程图: 3.适配服务器机型: 4.演示效果: 5.实现代码: #!/usr/bin/env bash # Author : JACK ZHAO # Date : ...

  10. Python全栈工程师(exercises)

    # # 练习: # # 1. 用map函数求: # # 1**3 + 2**3 + 3 ** 3 + .... 9**3 的和 def add_(x): return x ** 3 print(sum ...