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 ...
随机推荐
- js 页面跳转 新窗口打开
页面跳转:Window.showModalDialog(url,width,height); 弹出一个html文档的模式对话框Parent.window.document.location.href ...
- C# 编程--数组
数组 可以帮我我们一次声明存储多个相同类型的变量.用来解决同一类大量数据在内存存储和运算的功能特点:连续.同一类数据数组定义==>赋值==>取值 定义: int[] n ...
- Centos防火墙设置与端口开放
前言 最近在部署项目的时候遇到了一些问题,阿里云主机要配置安全组策略和端口.对于这点看到了一片好的博文,特此总结记录下. iptables 方法一 打开某个端口 // 开启端口 iptables -A ...
- Redis数据库总结
1.Redis简介 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI ...
- Java Exception异常介绍
一:介绍java异常 异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwab ...
- #2560异或和问题 jdfz集训—秦岳
题目描述 N个数字,要求选择M次,每次从N个数中选出两个数(Ai,Aj)(但不能和之前某次选择相同),此次选择的得分为Ai xor Aj. 求最大得分. 输入格式 第一行包含两个整数N,M 接下来一行 ...
- 【leetcode】424. Longest Repeating Character Replacement
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
- layout -panel01
<script src="~/jquery-easyui-1.5.5.2/jquery.min.js"></script> <link href=&q ...
- Nacos 常见问题及解决方法
Nacos 开源至今已有一年,在这一年里,得到了很多用户的支持和反馈.在与社区的交流中,我们发现有一些问题出现的频率比较高,为了能够让用户更快的解决问题,我们总结了这篇常见问题及解决方法,这篇文章后续 ...
- POJ 2387 Til the Cows Come Home (dijkstra模板题)
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...