27.Remove Element---两指针
题目链接: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---两指针的更多相关文章
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- 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 ...
- 【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 ...
- 27. Remove Element@python
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- (Array)27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- 27. Remove Element[E]移除元素
题目 Given an array nums and a value val, remove all instances of that value in-place and return the n ...
随机推荐
- [CodeForces940E]Cashback(set+DP)
Description Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is ...
- Apache 设置二级域名
开启重写模块 LoadModule rewrite_module modules/mod_rewrite.so 编辑配置 NameVirtualHost *:80 <VirtualHost *: ...
- 【UE4】二十四、UE4内部版本引擎和官方版本引擎版本保持兼容的方法
内部使用的引擎和官方正式发布的引擎版本号不一致,这种情况会导致一些插件由于版本不一致无法使用,有其是在没有插件源码的情况下.解决方法为 修改Engine\Source\Runtime\Launch\R ...
- 13,发布CRM
发布CRM你将使用以下软件 nginx uWSGI CentOS7 CRM项目文件 virtualenv supervisor WSGI.uWSGI python web服务器开发使用WSGI协议(W ...
- Android开发——View滑动的三种实现方式
0. 前言 Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...
- python考点
Python考点 1.Python类继承,内存管理(阿里) 答:内存管理机制包括:引用计数机制,垃圾回收机制,内存池机制 a = 1,1就是对象,a就是引用,引用a指向对象1. 2.Python装饰器 ...
- SecureCRT自动登录跳板机/堡垒机并连接目标机器
公司登录目标服务器,需要先登录跳板机(堡垒机),然后再登录目标机器,一共需要4.5步. MAC或LINUX机器可以写.SH脚本,那WINDOWS有没有一键登陆的方法呢? 常用的SecureCRT工具就 ...
- 测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低)
测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低) 答案: .A .C .C .A .A .D
- Python作业--登录接口
作业需求: 编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 实现思路: 1.从文件获取用户名密码 2.判断是否在黑名单中 3.验证用户名密码 成功:输出认证成功 错误:判断验证次 ...
- python-线程进程与队列
线程,有时被称为轻量级进程,是程序执行流的最小单元线程是程序中一个单一的顺序控制流程.进程内一个相对独立的.可调度的执行单元,是系统独立调度和分派CPU的基本单位指进行中的程序的调度单位.在单个程序中 ...