Skip to content

Commit 149d2d4

Browse files
committed
change 5-10 to md
1 parent 5ae740f commit 149d2d4

6 files changed

+522
-0
lines changed
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# 10. Regular Expression Matching
2+
3+
- Difficulty: Hard.
4+
- Related Topics: String, Dynamic Programming, Backtracking.
5+
- Similar Questions: Wildcard Matching.
6+
7+
## Problem
8+
9+
Given an input string (```s```) and a pattern (```p```), implement regular expression matching with support for ```'.'``` and ```'*'```.
10+
11+
```
12+
'.' Matches any single character.
13+
'*' Matches zero or more of the preceding element.
14+
```
15+
16+
The matching should cover the **entire** input string (not partial).
17+
18+
**Note:**
19+
20+
- ```s``` could be empty and contains only lowercase letters ```a-z```.
21+
- ```p``` could be empty and contains only lowercase letters ```a-z```, and characters like ```.``` or ```*```.
22+
23+
**Example 1:**
24+
25+
```
26+
Input:
27+
s = "aa"
28+
p = "a"
29+
Output: false
30+
Explanation: "a" does not match the entire string "aa".
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input:
37+
s = "aa"
38+
p = "a*"
39+
Output: true
40+
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
41+
```
42+
43+
**Example 3:**
44+
45+
```
46+
Input:
47+
s = "ab"
48+
p = ".*"
49+
Output: true
50+
Explanation: ".*" means "zero or more (*) of any character (.)".
51+
```
52+
53+
**Example 4:**
54+
55+
```
56+
Input:
57+
s = "aab"
58+
p = "c*a*b"
59+
Output: true
60+
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
61+
```
62+
63+
**Example 5:**
64+
65+
```
66+
Input:
67+
s = "mississippi"
68+
p = "mis*is*p*."
69+
Output: false
70+
```
71+
72+
## Solution
73+
74+
```javascript
75+
/**
76+
* @param {string} s
77+
* @param {string} p
78+
* @return {boolean}
79+
*/
80+
var isMatch = function(s, p) {
81+
var dp = Array(s.length + 1).fill(0).map(_ => Array(p.length + 1));
82+
return helper(dp, 0, 0, s, p);
83+
};
84+
85+
var helper = function (dp, i, j, s, p) {
86+
var res = false;
87+
if (dp[i][j] !== undefined) return dp[i][j];
88+
if (j === p.length) {
89+
res = i === s.length;
90+
} else {
91+
if (i === s.length) {
92+
res = p[j + 1] === '*' && helper(dp, i, j + 2, s, p);
93+
} else if (s[i] === p[j] || p[j] === '.') {
94+
if (p[j + 1] === '*') {
95+
res = helper(dp, i + 1, j, s, p) || helper(dp, i, j + 2, s, p) || helper(dp, i + 1, j + 2, s, p);
96+
} else {
97+
res = helper(dp, i + 1, j + 1, s, p);
98+
}
99+
} else {
100+
res = p[j + 1] === '*' && helper(dp, i, j + 2, s, p);
101+
}
102+
}
103+
dp[i][j] = res;
104+
return res;
105+
};
106+
```
107+
108+
**Explain:**
109+
110+
动态规划,```dp[i][j]``` 代表 ```s[i]``````p[j]``` 是否可匹配。
111+
112+
**Complexity:**
113+
114+
* Time complexity : O(mn).
115+
* Space complexity : O(mn).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 5. Longest Palindromic Substring
2+
3+
- Difficulty: Medium.
4+
- Related Topics: String, Dynamic Programming.
5+
- Similar Questions: Shortest Palindrome, Palindrome Permutation, Palindrome Pairs, Longest Palindromic Subsequence, Palindromic Substrings.
6+
7+
## Problem
8+
9+
Given a string **s**, find the longest palindromic substring in **s**. You may assume that the maximum length of **s** is 1000.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: "babad"
15+
Output: "bab"
16+
Note: "aba" is also a valid answer.
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: "cbbd"
23+
Output: "bb"
24+
```
25+
26+
## Solution
27+
28+
```javascript
29+
/**
30+
* @param {string} s
31+
* @return {string}
32+
*/
33+
var longestPalindrome = function(s) {
34+
var start = 0;
35+
var end = 0;
36+
var len = s.length;
37+
var num = 0;
38+
for (var i = 0; i < len; i++) {
39+
num = Math.max(expandAroundCenter(s, i, i), expandAroundCenter(s, i, i + 1));
40+
if (num > end - start) {
41+
start = i - Math.floor((num - 1) / 2);
42+
end = i + Math.floor(num / 2);
43+
}
44+
}
45+
return s.slice(start, end + 1);
46+
};
47+
48+
var expandAroundCenter = function (s, left, right) {
49+
var l = left;
50+
var r = right;
51+
var len = s.length;
52+
while (l >= 0 && r < len && s[l] === s[r]) {
53+
l--;
54+
r++;
55+
}
56+
return r - l - 1;
57+
};
58+
```
59+
60+
**Explain:**
61+
62+
遍历每个字符,检查以这个字符或相邻两个字符为中心是否回文,记录最长的回文字符位置。
63+
64+
**Complexity:**
65+
66+
* Time complexity : O(n^2).
67+
* Space complexity : O(1).

001-100/6. ZigZag Conversion.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 6. ZigZag Conversion
2+
3+
- Difficulty: Medium.
4+
- Related Topics: String.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
The string ```"PAYPALISHIRING"``` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
10+
11+
```
12+
P A H N
13+
A P L S I I G
14+
Y I R
15+
```
16+
17+
And then read line by line: ```"PAHNAPLSIIGYIR"```
18+
19+
Write the code that will take a string and make this conversion given a number of rows:
20+
21+
```
22+
string convert(string s, int numRows);
23+
```
24+
25+
**Example 1:**
26+
27+
```
28+
Input: s = "PAYPALISHIRING", numRows = 3
29+
Output: "PAHNAPLSIIGYIR"
30+
```
31+
32+
**Example 2:**
33+
34+
```
35+
Input: s = "PAYPALISHIRING", numRows = 4
36+
Output: "PINALSIGYAHRPI"
37+
Explanation:
38+
39+
P I N
40+
A L S I G
41+
Y A H R
42+
P I
43+
```
44+
45+
## Solution
46+
47+
```javascript
48+
/**
49+
* @param {string} s
50+
* @param {number} numRows
51+
* @return {string}
52+
*/
53+
var convert = function(s, numRows) {
54+
if (s.length <= numRows || numRows < 2) return s;
55+
var len = s.length;
56+
var num = 2 * (numRows - 1);
57+
var res = Array(numRows).fill('');
58+
var tmp = 0;
59+
for (var i = 0; i < len; i++) {
60+
tmp = i % num;
61+
if (tmp < numRows) {
62+
res[tmp] += s[i];
63+
} else {
64+
res[num - tmp] += s[i];
65+
}
66+
}
67+
return res.join('');
68+
};
69+
```
70+
71+
**Explain:**
72+
73+
曲线每 ```2 * (numRows - 1)``` 个数按规律出现。
74+
75+
**Complexity:**
76+
77+
* Time complexity : O(n).
78+
* Space complexity : O(n).

001-100/7. Reverse Integer.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 7. Reverse Integer
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Math.
5+
- Similar Questions: String to Integer (atoi).
6+
7+
## Problem
8+
9+
Given a 32-bit signed integer, reverse digits of an integer.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: 123
15+
Output: 321
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: -123
22+
Output: -321
23+
```
24+
25+
**Example 3:**
26+
27+
```
28+
Input: 120
29+
Output: 21
30+
```
31+
32+
**Note:**
33+
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
34+
35+
## Solution
36+
37+
```javascript
38+
/**
39+
* @param {number} x
40+
* @return {number}
41+
*/
42+
var reverse = function(x) {
43+
var INT_MAX = 2147483647;
44+
var INT_MIN = - INT_MAX - 1;
45+
var res = 0;
46+
var num = x;
47+
while (num !== 0) {
48+
res = (res * 10) + (num % 10);
49+
num = num > 0 ? Math.floor(num / 10) : Math.ceil(num / 10);
50+
if (res > INT_MAX || res < INT_MIN) return 0;
51+
}
52+
return res;
53+
};
54+
```
55+
56+
**Explain:**
57+
58+
nope.
59+
60+
**Complexity:**
61+
62+
* Time complexity : O(log(n)).
63+
* Space complexity : O(n).

0 commit comments

Comments
 (0)