Remove Element
FakeStandard
Posted on October 11, 2022
#27.Remove Element
Problem statement
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The relative order of the elements may be changed.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums
. More formally, if there are k
elements after removing the duplicates, then the first k
elements of nums
should hold the final result. It does not matter what you leave beyond the first k
elements.
Return k
after placing the final result in the first k
slots of nums
.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
// It is sorted with no values equaling val.
int k = removeElement(nums, val); // Calls your implementation
assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).
Explanation
給定一個整數陣列 nums
和整數 val
,我們必須就地刪除在 nums
內出現的 val
元素,使得每個元素只出現一次,而元素對應的順序需保持不變,返回剩餘元素的數量(經由測試,我猜測應該是返回剩餘元素數量,如果有誤還請告知)
限制只能使用額外的 O(1) 的記憶體空間,另外,因為某些語言宣告陣列後無法改變其長度,所以最終檢查時只會檢查前半段結果的部分,而陣列後半段非結果的部分不會檢查,可任意留下任何值,題目也附上如何檢查陣列的程式碼,它僅僅只檢查到我們要返回的結果值以前的元素
Solution
只是刪除特定元素很簡單,不過題目限制只能使用 O(1) 的額外空間,不能額外建立一個陣列來儲存元素,那麼只能直接遍歷題目給的陣列,並改變其陣列內的值,建立一個 res
變數來計數剩餘元素個數
使用迴圈將 nums[i]
和 val
比較值是否相同,若不相同就將陣列 i
索引的值指給 res
索引的位置,以此來移動陣列內的元素,若兩者相同則繼續迴圈,最後返回 res
public int RemoveElement(int[] nums, int val)
{
int res = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] != val)
{
nums[res] = nums[i];
res++;
}
}
return res;
}
或者再精簡一點的寫法,移除 res++
,並將 nums[res]
替換成 nums[res++]
,運作的方式相同,存取 res
索引結束後再執行增量
public int RemoveElement(int[] nums, int val)
{
int res = 0;
for (int i = 0; i < nums.Length; i++)
if (nums[i] != val)
nums[res++] = nums[i]; // shorter writing.
return res;
}
Reference
Thanks for reading the article 🌷 🌻 🌼
If you like it, please don't hesitate to click heart button ❤️
or click like on my Leetcode solution
or follow my GitHub ⭐ I'd appreciate it.
Posted on October 11, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.