Given an array nums and a value val, remove all instances of that value in-place and return the new length.html
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.java
The order of elements can be changed. It doesn't matter what you leave beyond the new length.python
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:this
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length = , with the first five elements of containing , , , , 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.5nums0130
Clarification:spa
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.code
Internally you can think of this:htm
// 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]); }
給定一個數組和一個值,移除全部和這個值相等的元素,返回新數組的長度。用in-place,O(1)空間。blog
解法:雙指針,從第一個元素開始遍歷,前面的指針i指向當前數,pos指針指向交換後的元素的下一位,若是和給定的數和當前數不等就和pos元素交換,last指針右移一位,繼續比較。
Java:
public class Solution { public int removeElement(int[] nums, int val) { int position = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != val) { nums[position] = nums[i]; position++; } } return position; } }
Python:
class Solution(object): def moveZero(self, nums, elem): i = 0 for j in xrange(len(nums)): if nums[j] != elem: nums[i], nums[j] = nums[j], nums[i] i += 1 return i
Python:
class Solution: def removeElement(self, A, elem): i, last = 0, len(A) - 1 while i <= last: if A[i] == elem: A[i], A[last] = A[last], A[i] last -= 1 else: i += 1 return last + 1
C++:
class Solution { public: int removeElement(vector<int>& nums, int val) { int res = 0; for (int i = 0; i < nums.size(); ++i) { if (nums[i] != val) nums[res++] = nums[i]; } return res; } };
變形:給一個字符數組,和一個字符,刪除數組中的因此給定的字符,並進行壓縮。M jia
壓縮就是全變成_____, eg: 數組是 abcdeba, 給的字符是 b, 刪除後數組變爲acdea__,後面兩個是空字符
void compressMe(char[] s, char c) { int index = 0; int i = 0; for (; i < s.length; i++){ if (s[i] != c){ s[index++] = s[i]; } } for (; index < s.length; index++){ s[index] = ' '; } }
C++:
private static void compressMe(char[] s, char c) { int insertIdx = 0; for (int i = 0; i < s.Length; i++) { if (s[i] != c) s[insertIdx++] = s[i]; } while (insertIdx < s.Length) s[insertIdx++] = ' '; }
Python: 一個循環裏
class Solution(object): def moveZero(self, nums, elem): i = 0 for j in xrange(len(nums)): if nums[j] != elem: nums[i], nums[j] = nums[j], '' i += 1 else: nums[j] = ''
相似題目:
[LeetCode] 283. Move Zeroes 移動零