【LeetCode算法-27】Remove Element
LeetCode第27题
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
翻译:
给定一个数组和一个定值,删除数组中的所有此值并返回一个新的数组长度
不要申明额外的数组空间,必须保证算法复杂度为O(1)
数组的顺序无所谓
思路:
这一题和【LeetCode算法-26】Remove Duplicates from Sorted Array差不多思路,只不过一个是删除重复值,一个是删除定值
既然不能申明额外的数组,那只能在原来的数组上做变动
变动前:[1,1,2,3,3],定值为:1
变动后:[2,3,3,3,3]
前3个值[2,3,3]就是我们所需要的
代码:
class Solution {
public int removeElement(int[] nums, int val) {
if(nums == null || nums.length == 0) return 0;
int j = 0;
for(int i = 0;i<nums.length;i++){
if(nums[i] != val){
nums[j] = nums[i];
System.out.println(nums[j]);
j++;
}
}
return j;
}
}
因为最后一次进入if(nums[i] != val)判断后,还是执行了一次j++,所以j的值就已经是数组长度了,return的时候不需要再+1了
欢迎关注我的微信公众号:安卓圈
【LeetCode算法-27】Remove Element的更多相关文章
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 【算法】LeetCode算法题-Remove Element
这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...
- 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 ...
- Leetcode No.27 Remove Element(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...
- 【LeetCode】27 - Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- LeetCode OJ 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
随机推荐
- 树莓派3b安装opencv
前言:最近买了一个CSI接口的摄像头,最准用树莓派做人脸识别项目.树莓派上本身已经安装了python2.python3,最开始通过sudo apt-get install python3-opencv ...
- MySQL/MariaDB数据库的复制监控和维护
MySQL/MariaDB数据库的复制监控和维护 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.清理日志 1>.删除指定日志文件名称之前的日志(也可用基于时间) M ...
- Docker万字详解!
一.简介 1.了解Docker的前生LXC LXC为Linux Container的简写.可以提供轻量级的虚拟化,以便隔离进程和资源,而且不需要提供指令解释机制以及全虚拟化的其他复杂性.相当于C+ ...
- js 预解析以及变量的提升
js在执行之前会进行预解析. 什么叫预解析? 预:提前 解析:编译 预解析通俗的说:js在执行代码之前会读取js代码,会将变量声明提前. 变量声明包含什么?1.var 声明 2.函数的显示声明. 提前 ...
- ora-00054资源正忙,但指定以nowait方式
select l.session_id,o.owner,o.object_name from v$locked_object l,dba_objects o where l.object_id=o.o ...
- C#线程池 ThreadPool
什么是线程池 大家都知道,我们在打开一个应用的时候,操作系统是要做很多的事情的,动态链接.装载.分配虚拟空间.等等等等,其实一个应用的打开同时也伴随着一个进程的建立. 进程的建立是需要时间的,在进程上 ...
- Chirp信号及其生成
Chirp信号是一个典型的非平稳信号,在通信.声纳.雷达等领域具有广泛的应用. 简介 Chirp译名:啁啾(读音:“周纠”),是通信技术有关编码脉冲技术中的一种术语,是指对脉冲进行编码时,其载频在脉冲 ...
- api的url规则设计,带参数的路由
api的url设计规则 router := gin.Default() router.GET("/topic/:topic_id", func(context *gin.Conte ...
- 洛谷 P2571 [SCOI2010]传送带 题解
每日一题 day51 打卡 Analysis 这道题是用非常恶心的三分套三分做的,有一个技巧是不要枚举坐标,枚举两条线段构成三角形的相似比就好了. 了解思路就还挺好写的(尽管我还调了三天) #incl ...
- 机器学习---三种线性算法的比较(线性回归,感知机,逻辑回归)(Machine Learning Linear Regression Perceptron Logistic Regression Comparison)
最小二乘线性回归,感知机,逻辑回归的比较: 最小二乘线性回归 Least Squares Linear Regression 感知机 Perceptron 二分类逻辑回归 Binary Logis ...