题目链接: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. java.math.BigDecimal cannot be cast to java.lang.String解决方法

    从mysql数据库里取decimal(18,2)封装到Map<String,String>中 BigDecimal b = new BigDecimal(resultMap.get(&qu ...

  2. 《Cracking the Coding Interview》——第11章:排序和搜索——题目8

    2014-03-21 22:23 题目:假设你一开始有一个空数组,你在读入一些整数并将其插入到数组中,保证插入之后数组一直按升序排列.在读入的过程中,你还可以进行一种操作:查询某个值val是否存在于数 ...

  3. USACO Section2.1 The Castle 解题报告

    castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  4. java日期格式化(util包下转成sql包下)

    package test; import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner; publ ...

  5. Java基础-3类和对象声明与创建

    一).在1和2中有粗略介绍过类和对象的概念,在这里简单回顾一下: 对象与类:一个实际或者虚拟的物体,这个物体既是我们的对象,这个物体呢又是属于一个分类,如动物类,人类 二).创建对象: 在创建对象的时 ...

  6. winform-实现类似QQ停靠桌面上边缘隐藏的效果

    //实现类似QQ停靠桌面上边缘隐藏的效果! private void timer1_Tick(object sender, EventArgs e) { System.Drawing.Point pp ...

  7. ironic baremetal rescue process

    1.用户调用Nova的rescue函数 nova/virt/ironic/driver.py class IronicDriver(virt_driver.ComputeDriver): ...... ...

  8. XGBoost——机器学习--周振洋

    XGBoost——机器学习(理论+图解+安装方法+python代码) 目录 一.集成算法思想 二.XGBoost基本思想 三.MacOS安装XGBoost 四.用python实现XGBoost算法 在 ...

  9. 简述Shiro验证过程

    如果让我们自己实现用户登录验证我们都需要哪些步骤? 很简单,根据用户提供的账号从数据库中查询该账户的密码以及一些其他信息,然后拿这个密码与用户输入的密码相比较,因为保存在数据库中的密码一般是经过加密的 ...

  10. iOS大神班笔记03-UIApplication

    UIApplication简介: UIApplication对象是应用程序的象征. 每一个应用程序都有自己的UIApplication对象,而且是单例. 一个iOS程序启动后创建的第一个对象就是UIA ...