LeetCode之路——LeetCode_27_Remove Element
来源:互联网 发布:网络销售期货合不合法 编辑:程序博客网 时间:2024/05/17 22:42
Given an array and a value, 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:
Given nums = [3,2,2,3], val = 3,Your function should return length = 2, with the first two elements of nums being 2.
思路:使用一次for循环,用index来计数(从0开始),返回的index就是整个数组中非val的值的个数。
class Solution {
public int removeElement(int[] nums, int val) {
int index = 0;
for(int i = 0;i < nums.length;i++){
if(nums[i] != val){
nums[index] = nums[i];
index++;
}
}
return index;
}
}
int index = 0;
for(int i = 0;i < nums.length;i++){
if(nums[i] != val){
nums[index] = nums[i];
index++;
}
}
return index;
}
}
阅读全文
0 0
- LeetCode之路——LeetCode_27_Remove Element
- Leetcode_27_Remove Element
- leetcode_27_Remove Element
- LeetCode—Majority Element
- leetcode刷题之旅——169. Majority Element
- LeetCode 之 Remove Element — C/C++实现
- LeetCode——Remove Element
- LeetCode——Remove Element
- LeetCode——Remove Element
- LeetCode——Majority Element
- Leetcode——remove-element
- leetcode——Majority Element
- LeetCode——Majority Element
- Find Peak Element—LeetCode
- leetcode之路027 Remove Element
- LeetCode进阶之路(Remove Element)
- LeetCode之路:169. Majority Element
- leetcode之Remove Element
- Java 位运算(移位、位与、或、异或、非) 以及负数的二进制相互计算
- spark算子实战
- Google说明
- 总结 2017.12.20
- NGUI使用Atlas制作图集
- LeetCode之路——LeetCode_27_Remove Element
- 使用Vue构建Ionic混合APP系列教程(三):服务和Http请求
- 数据库事务的四大特性以及事务的隔离级别
- 齿科服务项目
- springboot系列教程(一)——第一个springboot程序
- 各组件总结
- 人脸识别算法(一)---eigenfaces
- iOS中几个常用协议 NSCopying/NSMutableCopying
- shell