Skip to content

Commit 39f0885

Browse files
committed
feat: ^_^
1 parent 60d129c commit 39f0885

File tree

74 files changed

+3747
-1
lines changed

Some content is hidden

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

74 files changed

+3747
-1
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ module.exports = {
2222
'import/prefer-default-export': 'off',
2323
'@typescript-eslint/no-use-before-define': 'off',
2424
'no-param-reassign': 'off',
25+
'no-bitwise': 'off',
2526
'default-case': 'off',
27+
'one-var-declaration-per-line': 'off',
28+
'one-var': 'off',
2629
'prefer-destructuring': 'off',
2730
'max-len': 'off',
2831
'no-plusplus': 'off',

101.对称二叉树.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=typescript
3+
*
4+
* [101] 对称二叉树
5+
*
6+
* https://leetcode-cn.com/problems/symmetric-tree/description/
7+
*
8+
* algorithms
9+
* Easy (57.05%)
10+
* Likes: 1745
11+
* Dislikes: 0
12+
* Total Accepted: 498.1K
13+
* Total Submissions: 872.9K
14+
* Testcase Example: '[1,2,2,3,4,4,3]'
15+
*
16+
* 给你一个二叉树的根节点 root , 检查它是否轴对称。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* 输入:root = [1,2,2,3,4,4,3]
24+
* 输出:true
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* 输入:root = [1,2,2,null,3,null,3]
31+
* 输出:false
32+
*
33+
*
34+
*
35+
*
36+
* 提示:
37+
*
38+
*
39+
* 树中节点数目在范围 [1, 1000] 内
40+
* -100 <= Node.val <= 100
41+
*
42+
*
43+
*
44+
*
45+
* 进阶:你可以运用递归和迭代两种方法解决这个问题吗?
46+
*
47+
*/
48+
49+
import { TreeNode } from './commons/Tree'
50+
51+
export
52+
// @lc code=start1
53+
function isSymmetric(root: TreeNode | null): boolean {
54+
function helper(a: TreeNode | null, b: TreeNode | null): boolean {
55+
if (!a && !b) return true
56+
if (!a || !b) return false
57+
if (a.val !== b.val) return false
58+
return helper(a.left, b.right) && helper(a.right, b.left)
59+
}
60+
61+
return helper(root!.left, root!.right)
62+
}
63+
// @lc code=end

12.整数转罗马数字.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* @lc app=leetcode.cn id=12 lang=typescript
3+
*
4+
* [12] 整数转罗马数字
5+
*
6+
* https://leetcode-cn.com/problems/integer-to-roman/description/
7+
*
8+
* algorithms
9+
* Medium (66.40%)
10+
* Likes: 794
11+
* Dislikes: 0
12+
* Total Accepted: 263K
13+
* Total Submissions: 396.1K
14+
* Testcase Example: '3'
15+
*
16+
* 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
17+
*
18+
*
19+
* 字符 数值
20+
* I 1
21+
* V 5
22+
* X 10
23+
* L 50
24+
* C 100
25+
* D 500
26+
* M 1000
27+
*
28+
* 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V +
29+
* II 。
30+
*
31+
* 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数
32+
* 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
33+
*
34+
*
35+
* I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
36+
* X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
37+
* C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
38+
*
39+
*
40+
* 给你一个整数,将其转为罗马数字。
41+
*
42+
*
43+
*
44+
* 示例 1:
45+
*
46+
*
47+
* 输入: num = 3
48+
* 输出: "III"
49+
*
50+
* 示例 2:
51+
*
52+
*
53+
* 输入: num = 4
54+
* 输出: "IV"
55+
*
56+
* 示例 3:
57+
*
58+
*
59+
* 输入: num = 9
60+
* 输出: "IX"
61+
*
62+
* 示例 4:
63+
*
64+
*
65+
* 输入: num = 58
66+
* 输出: "LVIII"
67+
* 解释: L = 50, V = 5, III = 3.
68+
*
69+
*
70+
* 示例 5:
71+
*
72+
*
73+
* 输入: num = 1994
74+
* 输出: "MCMXCIV"
75+
* 解释: M = 1000, CM = 900, XC = 90, IV = 4.
76+
*
77+
*
78+
*
79+
* 提示:
80+
*
81+
*
82+
* 1
83+
*
84+
*
85+
*/
86+
87+
export
88+
// @lc code=start
89+
function intToRoman(num: number): string {
90+
let result = ''
91+
for (let i = 0; i < NUMS.length; i++) {
92+
while (num >= NUMS[i]) {
93+
num -= NUMS[i]
94+
result += DIGITS[i]
95+
}
96+
}
97+
return result
98+
}
99+
100+
const NUMS = [
101+
1000, 900, 500, 400,
102+
100, 90, 50, 40,
103+
10, 9, 5, 4,
104+
1,
105+
]
106+
const DIGITS = [
107+
'M', 'CM', 'D', 'CD',
108+
'C', 'XC', 'L', 'XL',
109+
'X', 'IX', 'V', 'IV',
110+
'I',
111+
]
112+
// @lc code=end
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* @lc app=leetcode.cn id=1239 lang=typescript
3+
*
4+
* [1239] 串联字符串的最大长度
5+
*
6+
* https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/description/
7+
*
8+
* algorithms
9+
* Medium (49.11%)
10+
* Likes: 194
11+
* Dislikes: 0
12+
* Total Accepted: 36.3K
13+
* Total Submissions: 73.8K
14+
* Testcase Example: '["un","iq","ue"]'
15+
*
16+
* 给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串,如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解。
17+
*
18+
* 请返回所有可行解 s 中最长长度。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
* 输入:arr = ["un","iq","ue"]
25+
* 输出:4
26+
* 解释:所有可能的串联组合是 "","un","iq","ue","uniq" 和 "ique",最大长度为 4。
27+
*
28+
*
29+
* 示例 2:
30+
*
31+
* 输入:arr = ["cha","r","act","ers"]
32+
* 输出:6
33+
* 解释:可能的解答有 "chaers" 和 "acters"。
34+
*
35+
*
36+
* 示例 3:
37+
*
38+
* 输入:arr = ["abcdefghijklmnopqrstuvwxyz"]
39+
* 输出:26
40+
*
41+
*
42+
*
43+
*
44+
* 提示:
45+
*
46+
*
47+
* 1 <= arr.length <= 16
48+
* 1 <= arr[i].length <= 26
49+
* arr[i] 中只含有小写英文字母
50+
*
51+
*
52+
*/
53+
54+
export
55+
// @lc code=start
56+
function maxLength(arr: string[]): number {
57+
const words = arr.map(getResp).filter((x) => x[0] > 0)
58+
let result = 0
59+
60+
function helper(prev: number, prevLength: number, i: number) {
61+
if (i >= words.length) {
62+
result = Math.max(result, prevLength)
63+
return
64+
}
65+
66+
helper(prev, prevLength, i + 1)
67+
68+
const [bits, length] = words[i]
69+
if ((prev & bits) === 0) {
70+
helper(prev | bits, prevLength + length, i + 1)
71+
}
72+
}
73+
helper(0, 0, 0)
74+
75+
return result
76+
}
77+
78+
function getResp(word: string) {
79+
let result = 0
80+
for (const char of word) {
81+
const code = char.charCodeAt(0) - 'a'.charCodeAt(0) + 1
82+
const digit = 1 << code
83+
if (result & digit) return [-1, -1]
84+
result |= digit
85+
}
86+
return [result, word.length]
87+
}
88+
// @lc code=end

0 commit comments

Comments
 (0)