Skip to content

Commit e967b33

Browse files
committed
note
1 parent c684972 commit e967b33

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

guangxu/202112/20211204.md

+67-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,76 @@
11
## Algorithm
22

3+
[27. Remove Element](https://leetcode.com/problems/remove-element/)
4+
35
### Description
46

5-
### Solution
7+
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.
8+
9+
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.
10+
11+
Return k after placing the final result in the first k slots of nums.
12+
13+
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
14+
15+
Custom Judge:
16+
17+
The judge will test your solution with the following code:
18+
19+
```
20+
int[] nums = [...]; // Input array
21+
int val = ...; // Value to remove
22+
int[] expectedNums = [...]; // The expected answer with correct length.
23+
// It is sorted with no values equaling val.
24+
25+
int k = removeElement(nums, val); // Calls your implementation
26+
27+
assert k == expectedNums.length;
28+
sort(nums, 0, k); // Sort the first k elements of nums
29+
for (int i = 0; i < actualLength; i++) {
30+
assert nums[i] == expectedNums[i];
31+
}
32+
```
33+
If all assertions pass, then your solution will be accepted.
634

7-
```java
35+
Example 1:
36+
37+
```
38+
Input: nums = [3,2,2,3], val = 3
39+
Output: 2, nums = [2,2,_,_]
40+
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
41+
It does not matter what you leave beyond the returned k (hence they are underscores).
42+
```
43+
44+
Example 2:
45+
46+
```
47+
Input: nums = [0,1,2,2,3,0,4,2], val = 2
48+
Output: 5, nums = [0,1,4,0,3,_,_,_]
49+
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
50+
Note that the five elements can be returned in any order.
51+
It does not matter what you leave beyond the returned k (hence they are underscores).
52+
```
53+
54+
Constraints:
55+
56+
- 0 <= nums.length <= 100
57+
- 0 <= nums[i] <= 50
58+
- 0 <= val <= 100
59+
60+
### Solution
861

62+
```java
63+
class Solution {
64+
public int removeElement(int[] nums, int val) {
65+
int j = 0;
66+
for(int i=0;i<nums.length;i++){
67+
if(nums[i]!=val){
68+
nums[j++]=nums[i];
69+
}
70+
}
71+
return j;
72+
}
73+
}
974
```
1075

1176
### Discuss

guangxu/202112/20211207.md

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class Solution {
6161

6262
### Discuss
6363

64+
1. 先排序再返回,复杂度至少为O(nlong)
65+
2. 注意返回的是数组下标,这里用map存储每个数字和下标元素
66+
6467
## Review
6568

6669

guangxu/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
| 标题 | Algorithm | Review | Tip | Share|
22
| - | - | - | - | - |
3+
| [20211207](./202112/20211207.md) |[1. Two Sum(两数之和)](https://leetcode.com/problems/two-sum/)||||
4+
| [20211206](./202112/20211206.md) |[128. Longest Consecutive Sequence(最长递增序列)](https://leetcode.com/problems/longest-consecutive-sequence/)||||
5+
| [20211205](./202112/20211205.md) |[4. Median of Two Sorted Arrays(两个排序数组的中位数)](https://leetcode.com/problems/median-of-two-sorted-arrays/)||||
6+
| [20211204](./202112/20211204.md) |[27. Remove Element(移除元素)](https://leetcode.com/problems/remove-element/)||||
37
| [20211203](./202112/20211203.md) |[81. Search in Rotated Sorted Array II(旋转数组中的查找2)](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)||||
48
| [20211202](./202112/20211202.md) |[704. Binary Search(二分查找)](https://leetcode.com/problems/binary-search/)||||
59
| [20211201](./202112/20211201.md) |[33. Search in Rotated Sorted Array(旋转数组中的查找)](https://leetcode.com/problems/search-in-rotated-sorted-array/)||||

0 commit comments

Comments
 (0)