LeetCode Array Easy 27. Remove Element 解题
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 = [,,,], val = , Your function should return length = , with the first two elements of nums being . It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [,,,,,,,], val = , Your function should return length = , with the first five elements of nums containing , , , , and . 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 = ; i < len; i++) {
print(nums[i]);
}
我的解题思路:遍历整个数组,逐个检查每个值是否与要求移除的值相同,如果相同,则将后面的值依次向前移动一个位置,并从当前位置重新开始检查。
遍历结束之后再检查最后一个位置的数是不是要求移除的值,如果是 返回值减1;
看代码:
public class Solution {
public int RemoveElement(int[] nums, int val) {
if(nums.Length == )
return ;
int i,j,length=nums.Length;
for(i = ; i < length; i++){
if(nums[i] == val){
for(j = i; j < length - ; j++){
nums[j] = nums[j+];
}
length--;
i--;//i递减1之后,从当前位置重新检查
}
}
if(length != && nums[length-] == val)//检查最后一个数字是不是需要移除的值
length--;
return length;
}
}
LeetCode Array Easy 27. Remove Element 解题的更多相关文章
- 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 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑
Description Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- (Array)27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode&Python] Problem 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-placeand return the new l ...
- LeetCode记录之27——Remove Element
这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. ive ...
- 【leetcode❤python】27. Remove Element
#-*- coding: UTF-8 -*- class Solution(object): def removeElement(self, nums, val): "& ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- 一文搞懂--Java中重写equals方法为什么要重写hashcode方法?
Java中重写equals方法为什么要重写hashcode方法? 直接看下面的例子: 首先我们只重写equals()方法 public class Test { public static void ...
- 使用雪花算法为分布式下全局ID、订单号等简单解决方案考虑到时钟回拨
1.snowflake简介 互联网快速发展的今天,分布式应用系统已经见怪不怪,在分布式系统中,我们需要各种各样的ID,既然是ID那么必然是要保证全局唯一,除此之外,不同当业务还需要不同 ...
- Java数组遍历
1.数组声明格式: 数据类型 [] 数组名 = new 数据类型[长度]: 数组长度一旦确定无法更改. 数组里的数据必须是相同类型或自动向上转型后兼容的类型 2.数组遍历 //一维数组 String ...
- jsonp详细原理之一
/*script标签是不存在跨域请求的,类似的还有img,background:url,link 你可以想象一下,平时的这些标签都是可以直接引入外部资源的,所以是不存在跨域问题的*/ function ...
- RxJava的学习与实现
RxJava 要在Android中使用RxJava2, 先添加Gradle配置: compile 'io.reactivex.rxjava2:rxjava:2.0.1' compile 'io.rea ...
- 【报错】解决logstash tracking_column not found in dataset. {:tracking_column=>"updated_time"}问题
今天用logstash同步数据库记录到elasticsearch时候出现错误,错误信息如下: [2019-10-12T23:51:00,529][WARN ][logstash.inputs.jdbc ...
- 使用IntelliJ IDEA 15和Maven创建Java Web项目(转)
转自:https://blog.csdn.net/myarrow/article/details/50824793 1. Maven简介 相对于传统的项目,Maven 下管理和构建的项目真的非常好用和 ...
- leetcode-165周赛-1277-统计全为1的正方形子矩阵
题目描述: 自己的提交: class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matri ...
- haproxy笔记
haproxy安装.启动.日志配置 方法1:#安装 yum install haproxy -y #日志配置 sed -i 's/^#$ModLoad imudp/$ModLoad imudp/g' ...
- mysql授权、删除用户和角色权限
备份权限 GRANT USAGE ON *.* TO 'backup'@'172.16.0.157' IDENTIFIED BY PASSWORD '*38B4F16EADB1601E713D9F03 ...