Skip to content

Commit 98cc82d

Browse files
committed
feat: add 026
1 parent b2bd355 commit 98cc82d

File tree

14 files changed

+101
-9
lines changed

14 files changed

+101
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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|
1819

1920

2021
## Medium
@@ -44,3 +45,4 @@
4445
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
4546
[020]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/020/README.md
4647
[021]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/021/README.md
48+
[026]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/026/README.md

note/007/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The input is assumed to be a 32-bit signed integer. Your function should **retur
2929

3030
## 思路
3131

32-
题意很清楚,就是给你一个整型数,返回它的逆序整型数,而且有个小坑点,当它的逆序整型数溢出的话,那么就返回0
32+
题意是给你一个整型数,求它的逆序整型数,而且有个小坑点,当它的逆序整型数溢出的话,那么就返回0
3333
,用我们代码表示的话可以求得结果保存在long中,最后把结果和整型的两个范围比较即可。
3434

3535
``` java

note/019/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Try to do this in one pass.
2020

2121
**Tags:** Linked List, Two Pointers
2222

23+
2324
## 思路
2425

2526
题意是让你删除链表中的倒数第n个数,我的解法是利用双指针,这两个指针相差n个元素,当后面的指针扫到链表末尾的时候,自然它前面的那个指针所指向的下一个元素就是要删除的元素,即`pre.next = pre.next.next;`,但是如果一开始后面的指针指向的为空,此时代表的意思就是要删除第一个元素,即`head = head.next;`

note/020/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The brackets must close in the correct order, `"()"` and `"()[]{}"` are all vali
88

99
**Tags:** Stack, String
1010

11+
1112
## 思路
1213

1314
题意是判断括号匹配是否正确,很明显,我们可以用栈来解决这个问题,当出现左括号的时候入栈,当遇到右括号时,判断栈顶的左括号是否何其匹配,不匹配的话直接返回`false`即可,最终判断是否空栈即可,这里我们可以用数组模拟栈的操作使其操作更快,有个细节注意下`top = 1;`,从而省去了之后判空的操作和`top - 1`导致数组越界的错误。

note/021/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
## Description
44

5-
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
5+
Given a sorted array, remove the duplicates in place such that each element appear only *once* 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+
For example,
10+
Given input array *nums* = `[1,1,2]`,
11+
12+
Your function should return length = `2`, with the first two elements of *nums* being `1` and `2` respectively. It doesn't matter what you leave beyond the new length.
613

714
**Tags:** Linked List
815

16+
917
## 思路
1018

1119
题意是用一个新链表来合并两个已排序的链表,那我们只需要从头开始比较已排序的两个链表,新链表指针每次指向值小的节点,依次比较下去,最后,当其中一个链表到达了末尾,我们只需要把新链表指针指向另一个没有到末尾的链表此时的指针即可。

note/026/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [Remove Duplicates from Sorted Array][title]
2+
3+
## Description
4+
5+
Given a sorted array, remove the duplicates in place such that each element appear only *once* 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+
For example,
10+
Given input array *nums* = `[1,1,2]`,
11+
12+
Your function should return length = `2`, with the first two elements of *nums* being `1` and `2` respectively. It doesn't matter what you leave beyond the new length.
13+
14+
**Tags:** Array, Two Pointers
15+
16+
17+
## 思路
18+
19+
题意是让你从一个有序的数组中移除重复的元素,并返回之后数组的长度。我的思路是判断长度小于等于1的话直接返回原长度即可,否则的话遍历一遍数组,用一个`tail`变量指向尾部,如果后面的元素和前面的元素不同,就让`tail`变量加一,最后返回tail即可。
20+
21+
``` java
22+
public class Solution {
23+
public int removeDuplicates(int[] nums) {
24+
int len = nums.length;
25+
if (len <= 1) return len;
26+
int tail = 1;
27+
for (int i = 1; i < len; ++i) {
28+
if (nums[i - 1] != nums[i]) {
29+
nums[tail++] = nums[i];
30+
}
31+
}
32+
return tail;
33+
}
34+
}
35+
```
36+
37+
38+
## 结语
39+
40+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
41+
42+
43+
44+
[title]: https://leetcode.com/problems/remove-duplicates-from-sorted-array
45+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_008/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* <pre>
55
* author: Blankj
66
* blog : http://blankj.com
7-
* time : 2017/04/21
7+
* time : 2017/04/23
88
* desc :
99
* </pre>
1010
*/

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_009/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* <pre>
55
* author: Blankj
66
* blog : http://blankj.com
7-
* time : 2017/04/22
7+
* time : 2017/04/24
88
* desc :
99
* </pre>
1010
*/

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_013/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* <pre>
88
* author: Blankj
99
* blog : http://blankj.com
10-
* time : 2017/04/22
10+
* time : 2017/04/25
1111
* desc :
1212
* </pre>
1313
*/

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_014/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* <pre>
88
* author: Blankj
99
* blog : http://blankj.com
10-
* time : 2017/04/21
10+
* time : 2017/04/26
1111
* desc :
1212
* </pre>
1313
*/

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_019/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* <pre>
88
* author: Blankj
99
* blog : http://blankj.com
10-
* time : 2017/04/21
10+
* time : 2017/04/27
1111
* desc :
1212
* </pre>
1313
*/

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_020/Solution.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* <pre>
88
* author: Blankj
99
* blog : http://blankj.com
10-
* time : 2017/04/21
10+
* time : 2017/04/28
1111
* desc :
1212
* </pre>
1313
*/
@@ -35,5 +35,7 @@ else if (c == '}' && stack[top - 1] != '{')
3535

3636
public static void main(String[] args) {
3737
Solution solution = new Solution();
38+
System.out.println(solution.isValid("()[]{}({[]})"));
39+
System.out.println(solution.isValid("(])]"));
3840
}
3941
}

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_021/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* <pre>
55
* author: Blankj
66
* blog : http://blankj.com
7-
* time : 2017/04/21
7+
* time : 2017/04/29
88
* desc :
99
* </pre>
1010
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.blankj.easy._026;
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 removeDuplicates(int[] nums) {
14+
int len = nums.length;
15+
if (len <= 1) return len;
16+
int tail = 1;
17+
for (int i = 1; i < len; ++i) {
18+
if (nums[i - 1] != nums[i]) {
19+
nums[tail++] = nums[i];
20+
}
21+
}
22+
return tail;
23+
}
24+
25+
public static void main(String[] args) {
26+
Solution solution = new Solution();
27+
int[] data = new int[]{0, 1, 1, 2, 3, 3, 3};
28+
int len = solution.removeDuplicates(data);
29+
for (int i = 0; i < len; i++) {
30+
System.out.print(data[i] + (i == len - 1 ? "" : ", "));
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)