Skip to content

Commit d3abe6b

Browse files
committed
feat: ^_^
Signed-off-by: Luozhou (Siyuan Cao) <luozhou.csy@alibaba-inc.com>
1 parent 398bc14 commit d3abe6b

20 files changed

+1922
-0
lines changed

10.正则表达式匹配.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* @lc app=leetcode.cn id=10 lang=typescript
3+
*
4+
* [10] 正则表达式匹配
5+
*
6+
* https://leetcode.cn/problems/regular-expression-matching/description/
7+
*
8+
* algorithms
9+
* Hard (31.69%)
10+
* Likes: 3152
11+
* Dislikes: 0
12+
* Total Accepted: 306.1K
13+
* Total Submissions: 966K
14+
* Testcase Example: '"aa"\n"a"'
15+
*
16+
* 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。
17+
*
18+
*
19+
* '.' 匹配任意单个字符
20+
* '*' 匹配零个或多个前面的那一个元素
21+
*
22+
*
23+
* 所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。
24+
*
25+
*
26+
* 示例 1:
27+
*
28+
*
29+
* 输入:s = "aa", p = "a"
30+
* 输出:false
31+
* 解释:"a" 无法匹配 "aa" 整个字符串。
32+
*
33+
*
34+
* 示例 2:
35+
*
36+
*
37+
* 输入:s = "aa", p = "a*"
38+
* 输出:true
39+
* 解释:因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
40+
*
41+
*
42+
* 示例 3:
43+
*
44+
*
45+
* 输入:s = "ab", p = ".*"
46+
* 输出:true
47+
* 解释:".*" 表示可匹配零个或多个('*')任意字符('.')。
48+
*
49+
*
50+
*
51+
*
52+
* 提示:
53+
*
54+
*
55+
* 1 <= s.length <= 20
56+
* 1 <= p.length <= 30
57+
* s 只包含从 a-z 的小写字母。
58+
* p 只包含从 a-z 的小写字母,以及字符 . 和 *。
59+
* 保证每次出现字符 * 时,前面都匹配到有效的字符
60+
*
61+
*
62+
*/
63+
64+
export
65+
// @lc code=start
66+
function isMatch(s: string, p: string): boolean {
67+
const m = s.length, n = p.length
68+
69+
const dp = generateArray(
70+
m + 1,
71+
() => generateArray(n + 1, () => false),
72+
)
73+
74+
for (let i = 0; i <= m; i++) {
75+
const si = i - 1
76+
77+
for (let j = 0; j <= n; j++) {
78+
const pj = j - 1
79+
80+
if (i === 0) {
81+
dp[i][j] = j === 0 || (p[pj] === '*' && dp[i][j - 2])
82+
} else if (j === 0) {
83+
dp[i][j] = false
84+
} else if (p[pj] === '*') {
85+
dp[i][j] = dp[i][j - 2] // match 0
86+
|| (dp[i - 1][j] && isCharMatch(s[si], p[pj - 1])) // match 1+
87+
} else {
88+
dp[i][j] = dp[i - 1][j - 1] && isCharMatch(s[si], p[pj])
89+
}
90+
}
91+
}
92+
93+
return dp[m][n]
94+
}
95+
96+
function isCharMatch(char: string, patternChar: string) {
97+
return char === patternChar || patternChar === '.'
98+
}
99+
100+
function generateArray<T>(n: number, generator: (i: number) => T) {
101+
return new Array(n).fill(0).map((_, i) => generator(i))
102+
}
103+
// @lc code=end
104+
105+
// console.log(isMatch('aa', 'a'))
106+
// console.log(isMatch('aa', 'a*'))
107+
// console.log(isMatch('ab', '.*'))
108+
109+
console.log(isMatch('ab', 'a.*b'))
110+
console.log(isMatch('ab', 'aa.*b'))
111+
112+
// 'aa'
113+
// 'a*'
114+
115+
// aa
116+
// a*
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* @lc app=leetcode.cn id=1239 lang=typescript
3+
*
4+
* [1239] 串联字符串的最大长度
5+
*
6+
* https://leetcode.cn/problems/maximum-length-of-a-concatenated-string-with-unique-characters/description/
7+
*
8+
* algorithms
9+
* Medium (48.93%)
10+
* Likes: 207
11+
* Dislikes: 0
12+
* Total Accepted: 38.2K
13+
* Total Submissions: 78.1K
14+
* Testcase Example: '["un","iq","ue"]'
15+
*
16+
* 给定一个字符串数组 arr,字符串 s 是将 arr 的含有 不同字母 的 子序列 字符串 连接 所得的字符串。
17+
*
18+
* 请返回所有可行解 s 中最长长度。
19+
*
20+
* 子序列 是一种可以从另一个数组派生而来的数组,通过删除某些元素或不删除元素而不改变其余元素的顺序。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
*
27+
* 输入:arr = ["un","iq","ue"]
28+
* 输出:4
29+
* 解释:所有可能的串联组合是:
30+
* - ""
31+
* - "un"
32+
* - "iq"
33+
* - "ue"
34+
* - "uniq" ("un" + "iq")
35+
* - "ique" ("iq" + "ue")
36+
* 最大长度为 4。
37+
*
38+
*
39+
* 示例 2:
40+
*
41+
*
42+
* 输入:arr = ["cha","r","act","ers"]
43+
* 输出:6
44+
* 解释:可能的解答有 "chaers" 和 "acters"。
45+
*
46+
*
47+
* 示例 3:
48+
*
49+
*
50+
* 输入:arr = ["abcdefghijklmnopqrstuvwxyz"]
51+
* 输出:26
52+
*
53+
*
54+
*
55+
*
56+
* 提示:
57+
*
58+
*
59+
* 1 <= arr.length <= 16
60+
* 1 <= arr[i].length <= 26
61+
* arr[i] 中只含有小写英文字母
62+
*
63+
*
64+
*/
65+
66+
export
67+
// @lc code=start
68+
function maxLength(arr: string[]): number {
69+
let result = 0
70+
const maskList = arr.map(getMask)
71+
72+
function walk(i: number, mask: number, currLength: number) {
73+
if (i >= maskList.length) {
74+
result = Math.max(result, currLength)
75+
return
76+
}
77+
78+
walk(i + 1, mask, currLength)
79+
80+
if (maskList[i] < 0) return
81+
if (maskList[i] & mask) return
82+
83+
walk(i + 1, mask | maskList[i], currLength + arr[i].length)
84+
}
85+
86+
walk(0, 0, 0)
87+
return result
88+
}
89+
90+
const CHAR_CODE_SMALL_A = 'a'.charCodeAt(0)
91+
92+
const getMask = (word: string) => {
93+
let result = 0
94+
95+
for (let i = 0; i < word.length; i++) {
96+
const digit = 1 << (word.charCodeAt(i) - CHAR_CODE_SMALL_A)
97+
if (result & digit) {
98+
return -1
99+
}
100+
result |= digit
101+
}
102+
103+
return result
104+
}
105+
// @lc code=end

125.验证回文串.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode.cn id=125 lang=typescript
3+
*
4+
* [125] 验证回文串
5+
*
6+
* https://leetcode.cn/problems/valid-palindrome/description/
7+
*
8+
* algorithms
9+
* Easy (46.89%)
10+
* Likes: 563
11+
* Dislikes: 0
12+
* Total Accepted: 394.3K
13+
* Total Submissions: 841K
14+
* Testcase Example: '"A man, a plan, a canal: Panama"'
15+
*
16+
* 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
17+
*
18+
* 说明:本题中,我们将空字符串定义为有效的回文串。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
*
25+
* 输入: "A man, a plan, a canal: Panama"
26+
* 输出: true
27+
* 解释:"amanaplanacanalpanama" 是回文串
28+
*
29+
*
30+
* 示例 2:
31+
*
32+
*
33+
* 输入: "race a car"
34+
* 输出: false
35+
* 解释:"raceacar" 不是回文串
36+
*
37+
*
38+
*
39+
*
40+
* 提示:
41+
*
42+
*
43+
* 1
44+
* 字符串 s 由 ASCII 字符组成
45+
*
46+
*
47+
*/
48+
49+
export
50+
// @lc code=start
51+
function isPalindrome(s: string): boolean {
52+
let i = 0, j = s.length - 1
53+
while (i < j) {
54+
while (i < j && !isAlphaNum(s[i])) {
55+
i++
56+
}
57+
while (i < j && !isAlphaNum(s[j])) {
58+
j--
59+
}
60+
if (i >= j) break
61+
if (!isSame(s[i], s[j])) {
62+
return false
63+
}
64+
65+
i++
66+
j--
67+
}
68+
69+
return true
70+
}
71+
72+
function isAlphaNum(char: string) {
73+
return (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9')
74+
}
75+
76+
function isSame(a: string, b: string) {
77+
return a.toLowerCase() === b.toLowerCase()
78+
}
79+
// @lc code=end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @lc app=leetcode.cn id=1304 lang=typescript
3+
*
4+
* [1304] 和为零的 N 个不同整数
5+
*
6+
* https://leetcode.cn/problems/find-n-unique-integers-sum-up-to-zero/description/
7+
*
8+
* algorithms
9+
* Easy (71.29%)
10+
* Likes: 67
11+
* Dislikes: 0
12+
* Total Accepted: 25.6K
13+
* Total Submissions: 36K
14+
* Testcase Example: '5'
15+
*
16+
* 给你一个整数 n,请你返回 任意 一个由 n 个 各不相同 的整数组成的数组,并且这 n 个数相加和为 0 。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
* 输入:n = 5
23+
* 输出:[-7,-1,1,3,4]
24+
* 解释:这些数组也是正确的 [-5,-1,1,2,3],[-3,-1,2,-2,4]。
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
* 输入:n = 3
30+
* 输出:[-1,0,1]
31+
*
32+
*
33+
* 示例 3:
34+
*
35+
* 输入:n = 1
36+
* 输出:[0]
37+
*
38+
*
39+
*
40+
*
41+
* 提示:
42+
*
43+
*
44+
* 1 <= n <= 1000
45+
*
46+
*
47+
*/
48+
49+
export
50+
// @lc code=start
51+
function sumZero(n: number): number[] {
52+
const result = new Array(n).fill(0).map((_, i) => i)
53+
result[0] = -((n * (n - 1)) >> 1)
54+
return result
55+
}
56+
// @lc code=end

0 commit comments

Comments
 (0)