Skip to content

Commit 9a49dd8

Browse files
committed
55. 56. 62. 64. 22.
1 parent 2ff480e commit 9a49dd8

8 files changed

+151
-43
lines changed

LeetcodeTop100/10. 正则表达式匹配.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public boolean isMatch(String s, String p) {
55
}
66
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
77
dp[0][0] = true;// dp[i][j] 表示 s 的前 i 个是否能被 p 的前 j 个匹配
8-
for (int i = 0; i < p.length(); i++) {
8+
for (int i = 1; i < p.length(); i++) {
99
if (p.charAt(i) == '*' && dp[0][i - 1]) {
1010
dp[0][i + 1] = true;
1111
}
@@ -14,17 +14,16 @@ public boolean isMatch(String s, String p) {
1414
for (int j = 0; j < p.length(); j++) {
1515
if (p.charAt(j) == '.' || p.charAt(j) == s.charAt(i)) {// 如果是任意元素 或者是对于元素匹配
1616
dp[i + 1][j + 1] = dp[i][j];
17-
}
18-
if (p.charAt(j) == '*') {
19-
if (p.charAt(j - 1) != s.charAt(i) && p.charAt(j - 1) != '.') {// 如果前一个元素不匹配 且不为任意元素
17+
} else if (p.charAt(j) == '*') {
18+
if (p.charAt(j - 1) != s.charAt(i) && p.charAt(j - 1) != '.') {// 如果前一个元素不匹配 且不为任意元素 没有匹配的情况
2019
dp[i + 1][j + 1] = dp[i + 1][j - 1];
2120
} else {
2221
// dp[i + 1][j + 1] = (dp[i + 1][j] || dp[i][j + 1] || dp[i + 1][j - 1]);
2322
dp[i + 1][j + 1] = (dp[i][j + 1] || dp[i + 1][j] || dp[i + 1][j - 1]);
2423
/*
2524
* dp[i][j] = dp[i-1][j] // 多个字符匹配的情况
26-
* or dp[i][j] = dp[i][j-1] // 单个字符匹配的情况
27-
* or dp[i][j] = dp[i][j-2] // 没有匹配的情况
25+
* or dp[i][j] = dp[i][j-1] // 单个字符匹配的情况
26+
* or dp[i][j] = dp[i][j-2] // 没有匹配的情况(虽然p.charAt(j - 1) == s.charAt(i)但是其实不能匹配,即为零个)
2827
*/
2928
}
3029
}

LeetcodeTop100/55. 跳跃游戏.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean canJump(int[] nums) {
3+
int lastPos = nums.length - 1;
4+
for(int i = nums.length - 1; i >= 0; i--){
5+
if(i + nums[i] >= lastPos){
6+
lastPos = i;
7+
}
8+
}
9+
return lastPos == 0;
10+
}
11+
}

LeetcodeTop100/56. 合并区间.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public int[][] merge(int[][] intervals) {
3+
if (intervals.length == 0) {
4+
return intervals;
5+
}
6+
int n = intervals.length;
7+
int[] starts = new int[n];
8+
int[] ends = new int[n];
9+
for (int i = 0; i < n; i++) {
10+
starts[i] = intervals[i][0];
11+
ends[i] = intervals[i][1];
12+
}
13+
Arrays.sort(starts);
14+
Arrays.sort(ends);
15+
int start = starts[0];
16+
int i = 0, j = 0;
17+
int count = 0;
18+
List<int[]> list = new ArrayList<>();
19+
while (i < n || j < n) {
20+
if (i < n && starts[i] <= ends[j]) {
21+
count++;
22+
if (count == 1) {
23+
start = starts[i];
24+
}
25+
i++;
26+
} else {
27+
count--;
28+
if (count == 0) {
29+
list.add(new int[] { start, ends[j] });
30+
}
31+
j++;
32+
}
33+
}
34+
return list.toArray(new int[list.size()][]);
35+
}
36+
}

LeetcodeTop100/62. 不同路径.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int uniquePaths(int m, int n) {
3+
int[][] result = new int[m][n];
4+
for (int i = 0; i < m; i++) {
5+
result[i][0] = 1;
6+
}
7+
for (int j = 0; j < n; j++) {
8+
result[0][j] = 1;
9+
}
10+
for (int i = 1; i < m; i++) {
11+
for (int j = 1; j < n; j++) {
12+
result[i][j] = result[i][j - 1] + result[i - 1][j];
13+
}
14+
}
15+
return result[m - 1][n - 1];
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int minPathSum(int[][] grid) {
3+
int m = grid.length;
4+
int n = grid[0].length;
5+
for (int i = 1; i < m; i++) {
6+
grid[i][0] += grid[i - 1][0];
7+
}
8+
for (int j = 1; j < n; j++) {
9+
grid[0][j] += grid[0][j - 1];
10+
}
11+
for (int i = 1; i < m; i++) {
12+
for (int j = 1; j < n; j++) {
13+
grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
14+
}
15+
}
16+
return grid[m - 1][n - 1];
17+
}
18+
}

README.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,55 @@
22

33
## LeetcodeTop100🔥
44
[题目链接👈](https://leetcode-cn.com/problemset/all/?listId=2cktkvj)
5-
- [1. 两数之和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/1.%20%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.java)
6-
- [2. 两数相加](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/2.%20%E4%B8%A4%E6%95%B0%E7%9B%B8%E5%8A%A0.java)
7-
- [3. 无重复字符的最长子串](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/3.%20%E6%97%A0%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E6%9C%80%E9%95%BF%E5%AD%90%E4%B8%B2.java)
5+
- [1. 两数之和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/1.%20两数之和.java)
6+
- [2. 两数相加](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/2.%20两数相加.java)
7+
- [3. 无重复字符的最长子串](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/3.%20无重复字符的最长子串.java)
88
- [思路](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/3.Ideas.md)
9-
- [4. 寻找两个有序数组的中位数](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/4.%20%E5%AF%BB%E6%89%BE%E4%B8%A4%E4%B8%AA%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E4%B8%AD%E4%BD%8D%E6%95%B0.java)
9+
- [4. 寻找两个有序数组的中位数](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/4.%20寻找两个有序数组的中位数.java)
1010
- [思路](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/4.Ideas.md)
11-
- [5. 最长回文子串](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/5.%20%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2.java)
11+
- [5. 最长回文子串](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/5.%20最长回文子串.java)
1212
- [思路](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/5.Ideas.md)
13-
- [10. 正则表达式匹配](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/10.%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%8C%B9%E9%85%8D.java)
14-
- [11. 盛最多水的容器](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/11.%20%E7%9B%9B%E6%9C%80%E5%A4%9A%E6%B0%B4%E7%9A%84%E5%AE%B9%E5%99%A8.java)
15-
- [15. 三数之和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/15.%20%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.java)
16-
- [17. 电话号码的字母组合](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/17.%20%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%E7%9A%84%E5%AD%97%E6%AF%8D%E7%BB%84%E5%90%88.java)
17-
- [19. 删除链表的倒数第N个节点](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/19.%20%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.java)
18-
- [20. 有效的括号](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/20.%20%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.java)
19-
- [21. 合并两个有序链表](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/21.%20%E5%90%88%E5%B9%B6%E4%B8%A4%E4%B8%AA%E6%9C%89%E5%BA%8F%E9%93%BE%E8%A1%A8.java)
20-
- [22. 括号生成](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/22.%20%E6%8B%AC%E5%8F%B7%E7%94%9F%E6%88%90.java)
13+
- [10. 正则表达式匹配](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/10.%20正则表达式匹配.java)
14+
- [11. 盛最多水的容器](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/11.%20盛最多水的容器.java)
15+
- [15. 三数之和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/15.%20三数之和.java)
16+
- [17. 电话号码的字母组合](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/17.%20电话号码的字母组合.java)
17+
- [19. 删除链表的倒数第N个节点](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/19.%20删除链表的倒数第N个节点.java)
18+
- [20. 有效的括号](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/20.%20有效的括号.java)
19+
- [21. 合并两个有序链表](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/21.%20合并两个有序链表.java)
20+
- [22. 括号生成](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/22.%20括号生成.java)
2121
- 23.合并K个排序链表
22-
- [23. 合并K个排序链表](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/23.%20%E5%90%88%E5%B9%B6K%E4%B8%AA%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8.java)
23-
- [23. 合并K个排序链表(堆排序)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/23.%20%E5%90%88%E5%B9%B6K%E4%B8%AA%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%EF%BC%88%E5%A0%86%E6%8E%92%E5%BA%8F%EF%BC%89.java)
24-
- [31. 下一个排列](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/31.%20%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%8E%92%E5%88%97.java)
22+
- [23. 合并K个排序链表](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/23.%20合并K个排序链表.java)
23+
- [23. 合并K个排序链表(堆排序)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/23.%20合并K个排序链表(堆排序).java)
24+
- [31. 下一个排列](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/31.%20下一个排列.java)
2525
- 32.最长有效括号
26-
- [32. 最长有效括号(dp)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/32.%20%E6%9C%80%E9%95%BF%E6%9C%89%E6%95%88%E6%8B%AC%E5%8F%B7.java)
27-
- [32. 最长有效括号(栈)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/32.%20%E6%9C%80%E9%95%BF%E6%9C%89%E6%95%88%E6%8B%AC%E5%8F%B7%EF%BC%88%E6%A0%88%EF%BC%89.java)
28-
- [32. 最长有效括号(空间O(1))](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/32.%20%E6%9C%80%E9%95%BF%E6%9C%89%E6%95%88%E6%8B%AC%E5%8F%B7%EF%BC%88%E7%A9%BA%E9%97%B4O%EF%BC%881%EF%BC%89%EF%BC%89.java)
29-
- [33. 搜索旋转排序数组](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100%/33.%20%E6%90%9C%E7%B4%A2%E6%97%8B%E8%BD%AC%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84.java)
30-
- [34. 在排序数组中查找元素的第一个和最后一个位置](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/34.%20%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.java)
31-
- [39. 组合总和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/39.%20%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.java)
32-
- [42. 接雨水](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/42.%20%E6%8E%A5%E9%9B%A8%E6%B0%B4.java)
26+
- [32. 最长有效括号(dp)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/32.%20最长有效括号.java)
27+
- [32. 最长有效括号(栈)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/32.%20最长有效括号(栈).java)
28+
- [32. 最长有效括号(空间O(1))](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/32.%20最长有效括号(空间O(1)).java)
29+
- [33. 搜索旋转排序数组](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100%/33.%20搜索旋转排序数组.java)
30+
- [34. 在排序数组中查找元素的第一个和最后一个位置](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/34.%20在排序数组中查找元素的第一个和最后一个位置.java)
31+
- [39. 组合总和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/39.%20组合总和.java)
32+
- [42. 接雨水](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/42.%20接雨水.java)
3333
- [46. 全排列](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/46.%20全排列.java)
34-
- [48. 旋转图像](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/48.%20%E6%97%8B%E8%BD%AC%E5%9B%BE%E5%83%8F.java)
35-
- [49. 字母异位词分组](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/49.%20%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D%E5%88%86%E7%BB%84.java)
36-
- [53. 最大子序和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/53.%20%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.java)
34+
- [48. 旋转图像](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/48.%20旋转图像.java)
35+
- [49. 字母异位词分组](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/49.%20字母异位词分组.java)
36+
- [53. 最大子序和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/53.%20最大子序和.java)
37+
- [55. 跳跃游戏](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/55.%20跳跃游戏.java)
38+
- [56. 合并区间](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/56.%20合并区间.java)
39+
- [62. 不同路径](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/62.%20不同路径.java)
40+
- [64. 最小路径和](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/64.%20最小路径和.java)
3741

3842
- 94.二叉树的中序遍历
3943

40-
- [94. 二叉树的中序遍历](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/94.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86.java)
41-
- [94. 二叉树的中序遍历(非递归,用栈)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/94.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%88%E9%9D%9E%E9%80%92%E5%BD%92%EF%BC%8C%E7%94%A8%E6%A0%88%EF%BC%89.java)
42-
- [94. 二叉树的中序遍历(非递归,莫里斯遍历)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/94.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%88%E9%9D%9E%E9%80%92%E5%BD%92%EF%BC%8C%E8%8E%AB%E9%87%8C%E6%96%AF%E9%81%8D%E5%8E%86%EF%BC%89.java)
43-
- [309. 最佳买卖股票时机含冷冻期](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/309.%20%E6%9C%80%E4%BD%B3%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E6%97%B6%E6%9C%BA%E5%90%AB%E5%86%B7%E5%86%BB%E6%9C%9F.java)
44+
- [94. 二叉树的中序遍历](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/94.%20二叉树的中序遍历.java)
45+
- [94. 二叉树的中序遍历(非递归,用栈)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/94.%20二叉树的中序遍历(非递归,用栈).java)
46+
- [94. 二叉树的中序遍历(非递归,莫里斯遍历)](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/94.%20二叉树的中序遍历(非递归,莫里斯遍历).java)
47+
- [309. 最佳买卖股票时机含冷冻期](https://github.com/Dagon0577/LeetCode/blob/master/LeetcodeTop100/309.%20最佳买卖股票时机含冷冻期.java)
4448
- [思路](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-lab/)
4549

4650
## 力扣每日一题
4751

4852
- [面试题13. 机器人的运动范围](https://github.com/Dagon0577/LeetCode/blob/master/力扣每日一题/面试题13.%20机器人的运动范围.java)
53+
- [22. 括号生成](https://github.com/Dagon0577/LeetCode/blob/master/力扣每日一题/22.%20括号生成.java)
4954

5055
## List
5156
- 📌[**Array**](https://github.com/Dagon0577/LeetCode#array)

Type/LeetCode_Array/Medium/918. Maximum Sum Circular Subarray.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
class Solution {
22
public int maxSubarraySumCircular(int[] A) {
3-
int S = 0; // S = sum(A)
3+
int S = 0;
44
for (int x : A) {
55
S += x;
66
}
7+
if (A.length == 1) {
8+
return A[0];
9+
}
710
int ans1 = kadane(A, 0, A.length - 1, 1);
8-
int ans2 = S + kadane(A, 1, A.length - 2, -1);
9-
//int ans3 = S + kadane(A, 0, A.length - 2, -1);
10-
return Math.max(ans1, Math.max(ans2, ans3));
11+
int ans2 = S + kadane(A, 0, A.length - 2, -1);
12+
// int ans3 = S + kadane(A, 0, A.length - 2, -1);
13+
return Math.max(ans1, ans2);
1114
}
1215

1316
public int kadane(int[] A, int i, int j, int sign) {
14-
// The maximum non-empty subarray for array
15-
// [sign * A[i], sign * A[i+1], ..., sign * A[j]]
1617
int ans = Integer.MIN_VALUE;
1718
int cur = Integer.MIN_VALUE;
1819
for (int k = i; k <= j; k++) {
1920
cur = sign * A[k] + Math.max(cur, 0);
20-
ans = Math.max(anx, cur);
21+
ans = Math.max(ans, cur);
2122
}
2223
return ans;
2324
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public List<String> generateParenthesis(int n) {
3+
List<String> res = new ArrayList<>();
4+
backTracking(res, "", 0, 0, n);
5+
return res;
6+
}
7+
8+
private void backTracking(List<String> res, String cur, int left, int right, int n) {
9+
if (cur.length() == n * 2) {
10+
res.add(cur);
11+
return;
12+
}
13+
14+
if (left < n) {
15+
backTracking(res, cur + '(', left + 1, right, n);
16+
}
17+
if (right < left) {
18+
backTracking(res, cur + ')', left, right + 1, n);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)