Skip to content

Commit 76999e1

Browse files
authored
Merge pull request #1 from Blankj/master
更新数据
2 parents d54abce + a1f6cd5 commit 76999e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1973
-206
lines changed

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,19 @@
6969
| :--- | :--------------------------------------- | :------------------------------- |
7070
| 2 | [Add Two Numbers][002] | Linked List, Math |
7171
| 3 | [Longest Substring Without Repeating Characters][003] | Hash Table, Two Pointers, String |
72-
| 5 | [Longest Palindromic Substring][005] | String |
72+
| 5 | [Longest Palindromic Substring][005] | String, Dynamic Programming |
7373
| 6 | [ZigZag Conversion][006] | String |
7474
| 8 | [String to Integer (atoi)][008] | Math, String |
75+
| 11 | [Container With Most Water][011] | Array, Two Pointers |
76+
| 12 | [Integer to Roman][012] | Math, String |
7577
| 15 | [3Sum][015] | Array, Two Pointers |
78+
| 15 | [3Sum Closest][016] | Array, Two Pointers |
7679
| 17 | [Letter Combinations of a Phone Number][017] | String, Backtracking |
80+
| 18 | [4Sum][018] | Array, Hash Table, Two Pointers |
7781
| 19 | [Remove Nth Node From End of List][019] | Linked List, Two Pointers |
82+
| 22 | [Generate Parentheses][022] | String, Backtracking |
83+
| 24 | [Swap Nodes in Pairs][024] | Linked List |
84+
| 29 | [Divide Two Integers][029] | Math, Binary Search |
7885
| 33 | [Search in Rotated Sorted Array][033] | Arrays, Binary Search |
7986
| 43 | [Multiply Strings][043] | Math, String |
8087
| 49 | [Group Anagrams][049] | Hash Table, String |
@@ -85,15 +92,16 @@
8592

8693
## Hard
8794

88-
| # | Title | Tag |
89-
| :--- | :--------------------------------- | :--------------------------------------- |
90-
| 4 | [Median of Two Sorted Arrays][004] | Array, Binary Search, Divide and Conquer |
91-
| 10 | [Regular Expression Matching][010] | String, Dynamic Programming, Backtracking |
92-
| 23 | [Merge k Sorted Lists][023] | Linked List, Divide and Conquer, Heap |
93-
| 25 | [Reverse Nodes in k-Group][025] | Linked List |
94-
| 44 | [Wildcard Matching][044] | String, Dynamic Programming, Backtracking, Greedy |
95-
| 57 | [Insert Interval][057] | Array, Sort |
96-
| 68 | [Text Justification][068] | String |
95+
| # | Title | Tag |
96+
| :--- | :--------------------------------------- | :--------------------------------------- |
97+
| 4 | [Median of Two Sorted Arrays][004] | Array, Binary Search, Divide and Conquer |
98+
| 10 | [Regular Expression Matching][010] | String, Dynamic Programming, Backtracking |
99+
| 23 | [Merge k Sorted Lists][023] | Linked List, Divide and Conquer, Heap |
100+
| 25 | [Reverse Nodes in k-Group][025] | Linked List |
101+
| 30 | [Substring with Concatenation of All Words][030] | Hash Table, Two Pointers, String |
102+
| 44 | [Wildcard Matching][044] | String, Dynamic Programming, Backtracking, Greedy |
103+
| 57 | [Insert Interval][057] | Array, Sort |
104+
| 68 | [Text Justification][068] | String |
97105

98106

99107

@@ -141,9 +149,16 @@
141149
[005]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/005/README.md
142150
[006]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/006/README.md
143151
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
152+
[011]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/011/README.md
153+
[012]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/012/README.md
144154
[015]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
155+
[016]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/016/README.md
145156
[017]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/017/README.md
157+
[018]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/018/README.md
146158
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
159+
[022]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/022/README.md
160+
[024]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/024/README.md
161+
[029]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/029/README.md
147162
[033]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/033/README.md
148163
[043]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/043/README.md
149164
[049]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/049/README.md
@@ -155,6 +170,7 @@
155170
[010]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/010/README.md
156171
[023]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/023/README.md
157172
[025]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/025/README.md
173+
[030]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/030/README.md
158174
[044]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/044/README.md
159175
[057]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/057/README.md
160176
[068]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/068/README.md

note/005/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Given a string **s**, find the longest palindromic substring in **s**. You may assume that the maximum length of **s** is 1000.
66

7-
**Example:**
7+
**Example 1:**
88

99
```
1010
Input: "babad"
@@ -14,15 +14,15 @@ Output: "bab"
1414
Note: "aba" is also a valid answer.
1515
```
1616

17-
**Example:**
17+
**Example 2:**
1818

1919
```
2020
Input: "cbbd"
2121
2222
Output: "bb"
2323
```
2424

25-
**Tags:** String
25+
**Tags:** String, Dynamic Programming
2626

2727

2828
## 思路 0

note/006/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,28 @@ And then read line by line: `"PAHNAPLSIIGYIR"`
1515
Write the code that will take a string and make this conversion given a number of rows:
1616

1717
```
18-
string convert(string text, int nRows);
18+
string convert(string s, int numRows);
1919
```
2020

21-
`convert("PAYPALISHIRING", 3)` should return `"PAHNAPLSIIGYIR"`.
21+
**Example 1:**
22+
23+
```
24+
Input: s = "PAYPALISHIRING", numRows = 3
25+
Output: "PAHNAPLSIIGYIR"
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: s = "PAYPALISHIRING", numRows = 4
32+
Output: "PINALSIGYAHRPI"
33+
Explanation:
34+
35+
P I N
36+
A L S I G
37+
Y A H R
38+
P I
39+
```
2240

2341
**Tags:** String
2442

note/008/README.md

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,62 @@
22

33
## Description
44

5-
Implement atoi to convert a string to an integer.
5+
Implement `atoi` which converts a string to an integer.
66

7-
**Hint:** Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
7+
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
88

9-
**Notes:** It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
9+
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
1010

11-
**Spoilers:**
11+
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
1212

13-
**Requirements for atoi:**
13+
If no valid conversion could be performed, a zero value is returned.
1414

15-
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
15+
**Note:**
1616

17-
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
17+
- Only the space character `' '` is considered as whitespace character.
18+
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2<sup>31</sup>, 2<sup>31</sup> − 1]. If the numerical value is out of the range of representable values, INT_MAX (2<sup>31</sup> − 1) or INT_MIN (−2<sup>31</sup>) is returned.
1819

19-
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
20+
**Example 1:**
21+
22+
```
23+
Input: "42"
24+
Output: 42
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: " -42"
31+
Output: -42
32+
Explanation: The first non-whitespace character is '-', which is the minus sign.
33+
Then take as many numerical digits as possible, which gets 42.
34+
```
35+
36+
**Example 3:**
37+
38+
```
39+
Input: "4193 with words"
40+
Output: 4193
41+
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
42+
```
43+
44+
**Example 4:**
2045

21-
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
46+
```
47+
Input: "words and 987"
48+
Output: 0
49+
Explanation: The first non-whitespace character is 'w', which is not a numerical
50+
digit or a +/- sign. Therefore no valid conversion could be performed.
51+
```
52+
53+
**Example 5:**
54+
55+
```
56+
Input: "-91283472332"
57+
Output: -2147483648
58+
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
59+
Thefore INT_MIN (−2^31) is returned.
60+
```
2261

2362
**Tags:** Math, String
2463

@@ -37,12 +76,13 @@ class Solution {
3776
}
3877
for (; i < len; ++i) {
3978
int tmp = str.charAt(i) - '0';
40-
if (tmp < 0 || tmp > 9)
41-
break;
42-
if (ans > Integer.MAX_VALUE / 10 || ans == Integer.MAX_VALUE / 10 && Integer.MAX_VALUE % 10 < tmp)
79+
if (tmp < 0 || tmp > 9) break;
80+
if (ans > Integer.MAX_VALUE / 10
81+
|| (ans == Integer.MAX_VALUE / 10 && (sign == 1 && tmp > 7 || sign == -1 && tmp > 8))) {
4382
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
44-
else
83+
} else {
4584
ans = ans * 10 + tmp;
85+
}
4686
}
4787
return sign * ans;
4888
}

note/009/README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@
22

33
## Description
44

5-
Determine whether an integer is a palindrome. Do this without extra space.
5+
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
66

7-
**Spoilers:**
7+
**Example 1:**
88

9-
**Some hints:**
9+
```
10+
Input: 121
11+
Output: true
12+
```
13+
14+
**Example 2:**
15+
16+
```
17+
Input: -121
18+
Output: false
19+
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
20+
```
1021

11-
Could negative integers be palindromes? (ie, -1)
22+
**Example 3:**
1223

13-
If you are thinking of converting the integer to string, note the restriction of using extra space.
24+
```
25+
Input: 10
26+
Output: false
27+
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
28+
```
1429

15-
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
30+
**Follow up:**
1631

17-
There is a more generic way of solving this problem.
32+
Coud you solve it without converting the integer to a string?
1833

1934
**Tags:** Math
2035

note/010/README.md

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,75 @@
22

33
## Description
44

5-
Implement regular expression matching with support for `'.'` and `'*'`.
5+
Given an input string (`s`) and a pattern (`p`), implement regular expression matching with support for `'.'` and `'*'`.
66

77
```
88
'.' Matches any single character.
99
'*' Matches zero or more of the preceding element.
10+
```
11+
12+
The matching should cover the **entire** input string (not partial).
13+
14+
**Note:**
15+
16+
- `s` could be empty and contains only lowercase letters `a-z`.
17+
- `p` could be empty and contains only lowercase letters `a-z`, and characters like `.` or `*`.
18+
19+
**Example 1:**
20+
21+
```
22+
Input:
23+
s = "aa"
24+
p = "a"
25+
Output: false
26+
Explanation: "a" does not match the entire string "aa".
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input:
33+
s = "aa"
34+
p = "a*"
35+
Output: true
36+
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
37+
```
38+
39+
**Example 3:**
40+
41+
```
42+
Input:
43+
s = "ab"
44+
p = ".*"
45+
Output: true
46+
Explanation: ".*" means "zero or more (*) of any character (.)".
47+
```
1048

11-
The matching should cover the entire input string (not partial).
49+
**Example 4:**
1250

13-
The function prototype should be:
14-
bool isMatch(const char *s, const char *p)
51+
```
52+
Input:
53+
s = "aab"
54+
p = "c*a*b"
55+
Output: true
56+
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
57+
```
1558

16-
Some examples:
17-
isMatch("aa", "a") → false
18-
isMatch("aa", "aa") → true
19-
isMatch("aaa", "aa") → false
20-
isMatch("aa", "a*") → true
21-
isMatch("aa", ".*") → true
22-
isMatch("ab", ".*") → true
23-
isMatch("aab", "c*a*b") → true
59+
**Example 5:**
60+
61+
```
62+
Input:
63+
s = "mississippi"
64+
p = "mis*is*p*."
65+
Output: false
2466
```
2567

2668
**Tags:** String, Dynamic Programming, Backtracking
2769

2870

2971
## 思路 0
3072

31-
题意是让让你从判断 `s` 字符串是否正则匹配于 `p`,这道题和[Wildcard Matching][044]很是相似,区别在于 `*`,通配符的 `*` 是可以随意出现的,跟前面字符没有任何关系,其作用是可以表示任意字符串;而正则匹配的 `*` 不能单独存在,前面必须具有一个字符,其意义是表明前面的这个字符个数可以是任意个数,包括 0 个。首先我们用递归的方式来实现,其思路如下:
73+
题意是让让你从判断 `s` 字符串是否正则匹配于 `p`,这道题和 [Wildcard Matching][044] 很是相似,区别在于 `*`,通配符的 `*` 是可以随意出现的,跟前面字符没有任何关系,其作用是可以表示任意字符串;而正则匹配的 `*` 不能单独存在,前面必须具有一个字符,其意义是表明前面的这个字符个数可以是任意个数,包括 0 个。首先我们用递归的方式来实现,其思路如下:
3274

3375
* 如果 `s``p` 都为空,那么返回 `true`
3476

note/011/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [Container With Most Water][title]
2+
3+
## Description
4+
5+
Given *n* non-negative integers *a1*, *a2*, ..., *an*, where each represents a point at coordinate (*i*, *ai*). *n* vertical lines are drawn such that the two endpoints of line *i* is at (*i*, *ai*) and (*i*, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
6+
7+
Note: You may not slant the container and *n* is at least 2.
8+
9+
**Tags:** Array, Two Pointers
10+
11+
12+
## 思路
13+
14+
题意是给你 *a1*, *a2*, ..., *an**n* 个数,代表 (*i*, *ai*) 坐标,让你从中找两个点与 x 轴围成的容器可以容纳最多的水。
15+
16+
不明白的话可以看数据为 `1 8 6 2 5 4 8 3 7` 所示的图。
17+
18+
![](https://raw.githubusercontent.com/Blankj/awesome-java-leetcode/master/note/011/water.png)
19+
20+
如果用暴力法求每种情况的结果,其时间复杂度为 O(n^2),相信肯定会超时,我们可以探索下是否有更巧妙的办法呢,题目的标签有双指针,是否就可以想到首尾各放一指针,然后根据条件来收缩。首先计算一次首尾构成的最大面积,然后分析下该移动哪个指针,如果移动大的那个指针的话,那样只会减小面积,所以我们要移动小的那个指针,小的那个指针移动到哪呢?当然是移动到大于之前的值的地方,否则面积不都比之前小么,然后继续更新最大值即可,借助如上分析写出如下代码应该不是什么难事了吧。
21+
22+
23+
```java
24+
class Solution {
25+
public int maxArea(int[] height) {
26+
int l = 0, r = height.length - 1;
27+
int max = 0, h = 0;
28+
while (l < r) {
29+
h = Math.min(height[l], height[r]);
30+
max = Math.max(max, (r - l) * h);
31+
while (height[l] <= h && l < r) ++l;
32+
while (height[r] <= h && l < r) --r;
33+
}
34+
return max;
35+
}
36+
}
37+
```
38+
39+
40+
## 结语
41+
42+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl]
43+
44+
45+
46+
[title]: https://leetcode.com/problems/container-with-most-water
47+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

note/011/water.png

41.2 KB
Loading

0 commit comments

Comments
 (0)