Skip to content

Commit d3aff2d

Browse files
committed
feat: add 027
1 parent 98cc82d commit d3aff2d

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
1616
|20|[Valid Parentheses][020]|Stack, String|
1717
|21|[Merge Two Sorted Lists][021]|Linked List|
18-
|26|[Remove Duplicates from Sorted Array][026]|Linked List|
18+
|26|[Remove Duplicates from Sorted Array][026]|Array, Two Pointers|
19+
|27|[Remove Element][027]|Array, Two Pointers|
1920

2021

2122
## Medium
@@ -46,3 +47,4 @@
4647
[020]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/020/README.md
4748
[021]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/021/README.md
4849
[026]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/026/README.md
50+
[027]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/027/README.md

note/026/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Your function should return length = `2`, with the first two elements of *nums*
1616

1717
## 思路
1818

19-
题意是让你从一个有序的数组中移除重复的元素,并返回之后数组的长度。我的思路是判断长度小于等于1的话直接返回原长度即可,否则的话遍历一遍数组,用一个`tail`变量指向尾部,如果后面的元素和前面的元素不同,就让`tail`变量加一,最后返回tail即可
19+
题意是让你从一个有序的数组中移除重复的元素,并返回之后数组的长度。我的思路是判断长度小于等于1的话直接返回原长度即可,否则的话遍历一遍数组,用一个`tail`变量指向尾部,如果后面的元素和前面的元素不同,就让`tail`变量加一,最后返回`tail`即可
2020

2121
``` java
2222
public class Solution {

note/027/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [Remove Element][title]
2+
3+
## Description
4+
5+
Given an array and a value, remove all instances of that value in place and return the new length.
6+
7+
Do not allocate extra space for another array, you must do this in place with constant memory.
8+
9+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
10+
11+
**Example:**
12+
13+
Given input array *nums* = `[3,2,2,3]`, *val* = `3`
14+
15+
Your function should return length = 2, with the first two elements of *nums* being 2.
16+
17+
**Tags:** Array, Two Pointers
18+
19+
20+
## 思路
21+
22+
题意是移除数组中值等于`val`的元素,并返回之后数组的长度,并且题目中指定空间复杂度为O(1),我的思路是用`tail`标记尾部,遍历该数组时当索引元素不等于`val`时,`tail`加一,尾部指向当前元素,最后返回`tail`即可。
23+
24+
``` java
25+
public class Solution {
26+
public int removeElement(int[] nums, int val) {
27+
int tail = 0;
28+
for (int i = 0, len = nums.length; i < len; ++i) {
29+
if (nums[i] != val) {
30+
nums[tail++] = nums[i];
31+
}
32+
}
33+
return tail;
34+
}
35+
}
36+
```
37+
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
42+
43+
44+
45+
[title]: https://leetcode.com/problems/remove-element
46+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.blankj.easy._027;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/04/21
8+
* desc :
9+
* </pre>
10+
*/
11+
12+
public class Solution {
13+
public int removeElement(int[] nums, int val) {
14+
int tail = 0;
15+
for (int i = 0, len = nums.length; i < len; ++i) {
16+
if (nums[i] != val) {
17+
nums[tail++] = nums[i];
18+
}
19+
}
20+
return tail;
21+
}
22+
23+
public static void main(String[] args) {
24+
Solution solution = new Solution();
25+
int[] data = new int[]{0, 3, 1, 1, 2, 3, 3, 3};
26+
int len = solution.removeElement(data, 3);
27+
for (int i = 0; i < len; i++) {
28+
System.out.print(data[i] + (i == len - 1 ? "" : ", "));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)