Input 是一個整數陣列nums
,還有一個整數val
,陣列長度介於0至100之間,陣列元素大小介於0至50之間,而整數值介於0至100之間。
Output 要求我們將整數陣列nums
中等於整數val
的元素刪除,最後回傳剩下多少個元素。
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).
雖然指定要在輸入的陣列中操作,但並沒有要求排序,因此只要用一左一右(左快右慢)的指標,逐步往陣列中間靠攏,左邊元素一等於就和右邊元素交換。
Runtime: 0 ms
Memory Usage: 41 MB
class Solution {
public int removeElement(int[] nums, int val) {
int len = nums.length, temp = 0;
int left = 0, right = len - 1;
while (left <= right) {
if (nums[left] == val) {
temp = nums[right];
nums[right--] = nums[left];
nums[left] = temp;
} else {
left++;
}
}
return left;
}
}
Input 是一個整數陣列nums
,有經過排序,陣列長度介於1至310^4之間,陣列元素大小介於-100至100之間。
Output 要求我們將整數陣列nums
中重複的元素刪除,並且一樣要有排序。
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
int k = removeDuplicates(nums); // Calls your implementation
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
這跟上一題一樣是要用two-pointer 的作法,要一快一慢的判斷,只是這次是從頭開始走起。
Runtime: 1 ms
Memory Usage: 43.8 MB
class Solution {
public int removeDuplicates(int[] nums) {
int counter = 1;
for (int i = 1, len = nums.length; i < len; i++) {
if (nums[i - 1] != nums[i]) {
nums[counter] = nums[i];
counter++;
}
}
return counter;
}
}
發現這英文我不太會翻譯...還是直接貼過來好了...
Given an array arr
, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1
.
After doing so, return the array.
Example 1:
Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]
Explanation:
- index 0 --> the greatest element to the right of index 0 is index 1 (18).
- index 1 --> the greatest element to the right of index 1 is index 4 (6).
- index 2 --> the greatest element to the right of index 2 is index 4 (6).
- index 3 --> the greatest element to the right of index 3 is index 4 (6).
- index 4 --> the greatest element to the right of index 4 is index 5 (1).
- index 5 --> there are no elements to the right of index 5, so we put -1.
Example 2:
Input: arr = [400]
Output: [-1]
Explanation: There are no elements to the right of index 0.
Constraints:
1 <= arr.length <= 104
1 <= arr[i] <= 105
這題需要的是反向思考,它要判斷是不是右邊最大的元素,那我們就從尾端開始判斷。
Runtime: 1 ms
Memory Usage: 45 MB
class Solution {
public int[] replaceElements(int[] arr) {
int len = arr.length, temp = 0, max = -1;
for (int i = len - 1; i >= 0; i--) {
temp = arr[i];
arr[i] = max;
max = temp > max ? temp : max;
}
return arr;
}
}
Input 是一個整數陣列nums
,陣列長度介於1至10^4之間,陣列元素大小介於-2^31至2^31-1之間。
Output 要求我們將整數陣列nums
中所有0的元素一致最後面,並且保持原本非0元素的順序。
Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:
Input: nums = [0]
Output: [0]
這題解法跟26題一模一樣...
Runtime: 2 ms
Memory Usage: 45 MB
class Solution {
public void moveZeroes(int[] nums) {
int len = nums.length, temp = 0;
for (int i = 0, j = 0; i < len; i++) {
if (nums[i] != 0) {
temp = nums[i];
nums[i] = nums[j];
nums[j++] = temp;
}
}
}
}
Input 是一個整數陣列nums
,陣列長度介於1至5000之間,陣列元素大小介於0至5000之間。
Output 要求我們將整數陣列nums
中所有偶數放到前面,所有奇數放到後面,不論是否依大小排序。
Example 1:
Input: nums = [3,1,2,4]
Output: [2,4,3,1]
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Example 2:
Input: nums = [0]
Output: [0]
這題我用了three-pointer 才解決,有兩個分別一左一右標示開頭和尾端,第三個從頭開始遍歷陣列,偶數就與左邊交換位置,奇數就和右邊交換位置。
Runtime: 1 ms
Memory Usage: 44 MB
class Solution {
public int[] sortArrayByParity(int[] nums) {
int len = nums.length;
int[] result = new int[len];
for (int i = 0, left = 0, right = len - 1; i < len; i++) {
if (nums[i] % 2 == 0) {
result[left++] = nums[i];
} else {
result[right--] = nums[i];
}
}
return result;
}
}
開始有點力不從心了阿,難道這就是初老的徵兆嗎...
不過這幾題有很多都是用two-pointer 的概念解題,Explore Card 裡面明明沒有講到阿!