LeetCode每日一题(2018-01-24)
2018-01-24 14:45
162 查看
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.
我的代码:public int removeElement(int[] nums, int val) {
int length = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) nums[length++] = nums[i];
}
return length;
}
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.
我的代码:public int removeElement(int[] nums, int val) {
int length = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) nums[length++] = nums[i];
}
return length;
}
相关文章推荐
- 每日一题之LeetCode移除元素 删除有序数组重复元素
- leetcode:111 Minimum Depth of Binary Tree-每日编程第三十题
- 每日AC--LeetCode-Candy
- leetcode 每日一题 Delete Node in a Linked List
- leetcode 每日一题 171. Excel Sheet Column Number
- LeetCode每日一题(2018-01-03)
- 每日进步之leetcode第16题C++实现代码(
- LeetCode每日一练(5) Longest Palindromic Substring C++版
- leetcode:70 Climbing Stairs-每日编程第十五题
- leetcode 每日一题 258. Add Digits
- 每日一练之Roman to integer & integer to Roman【LeetCode No.12,13】
- leetcode:299 Bulls and Cows-每日编程第四十一题
- 每日一练之poor pigs【leetcode No.458】——猪测毒问题
- leetcode每日解题思路 221 Maximal Square
- LeetCode每日一题(2018-01-22)
- LeetCode每日一练 阿拉伯数字转罗马数字
- leetcode 每日一题 242. Valid Anagram
- leetcode:101 Symmetric Tree-每日编程第二十题
- leetcode:118 Pascal's Triangle-每日编程第二十四题
- LeetCode每日一题——412. Fizz Buzz