Two Pointers (my solution)
1 | class Solution { |
Remarks:
- TC: $O(n)$, SC: $O(1)$
Swap
1 | class Solution { |
Remarks:
TC: $O(n)$, SC: $O(1)$
Only runs when the sequence does not matter:
the OJ provides the dudging method:
1
2
3
4
5
6
7
8
9
10
11
12int[] 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];
}where the list is being sorted again, so sequence does not matter.
Simplified
1 | class Solution { |