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 ...
随机推荐
- The Road to learn React书籍学习笔记(第一章)
react灵活的生态圈 Small Application Boilerplate: create-react-app Utility: JavaScript ES6 and beyond Styli ...
- Android自定义组件之简单组合
Android自定义控件有两种,一种是组合.比如一个linearlayout 里面有textview,imageview. 这样的好处是,写一个就可以多处使用. view_image_and_butt ...
- 容器技术的落地还要依靠SDN
容器能够实现新应用程序的快速部署,代表着目前IT开发社区的最热门趋势之一.然而,想要实现容器部署生产环境,IT人员还需要使用SDN技术,在分布式微应用程序之间实现可扩展.可管理且安全的通信. 什么是容 ...
- LC.exe已退出,代码为-1
解决方法就是把Properties文件下的license.licx给删除,重新编译,这样就可以了.
- PJMEDIA之录音器的使用(capture sound to avi file)
为了熟悉pjmedia的相关函数以及使用方法,这里练习了官网上的一个录音器的例子. 核心函数: pj_status_t pjmedia_wav_writer_port_create ( pj_pool ...
- 《Cracking the Coding Interview》——第17章:普通题——题目14
2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
- 常见算法用Pascal实现
基本算法 这些都是非常基本的的算法,希望所有学习的人都能理解! 1.数论算法 求两数的最大公约数 function gcd(a,b:integer):integ ...
- 自动using和Layout
一.自动using 1. Model 文件夹添加 Person类,在view文件夹下web.config文件,将namespace加入,cshtml文件就不需要添加@model引用: ...
- RAID介绍和实现
RAID的全称是廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks),于1987年由美国Berkeley 大学的两名工程师提出的. RAID出现的,最初目的是将 ...
- CSU-1986 玄学
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1986 题目 Description 阴阳师子浩君,最近从<初等数论 ...