Skip to content

Commit 398bc14

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

20 files changed

+1917
-0
lines changed

12.整数转罗马数字.ts

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=12 lang=typescript
3+
*
4+
* [12] 整数转罗马数字
5+
*
6+
* https://leetcode.cn/problems/integer-to-roman/description/
7+
*
8+
* algorithms
9+
* Medium (66.19%)
10+
* Likes: 946
11+
* Dislikes: 0
12+
* Total Accepted: 321.5K
13+
* Total Submissions: 485.7K
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+
const result: string[] = []
91+
92+
for (let i = CHARS.length - 1; i >= 0; i--) {
93+
while (num >= VALUE[i]) {
94+
result.push(CHARS[i])
95+
num -= VALUE[i]
96+
}
97+
}
98+
99+
return result.join('')
100+
}
101+
102+
/* eslint-disable no-multi-spaces, array-bracket-spacing */
103+
const CHARS = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M']
104+
const VALUE = [ 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
105+
// @lc code=end

173.二叉搜索树迭代器.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* @lc app=leetcode.cn id=173 lang=typescript
3+
*
4+
* [173] 二叉搜索树迭代器
5+
*
6+
* https://leetcode.cn/problems/binary-search-tree-iterator/description/
7+
*
8+
* algorithms
9+
* Medium (81.29%)
10+
* Likes: 634
11+
* Dislikes: 0
12+
* Total Accepted: 100.5K
13+
* Total Submissions: 123.7K
14+
* Testcase Example: '["BSTIterator","next","next","hasNext","next","hasNext","next","hasNext","next","hasNext"]\n' +
15+
'[[[7,3,15,null,null,9,20]],[],[],[],[],[],[],[],[],[]]'
16+
*
17+
* 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器:
18+
*
19+
*
20+
*
21+
* BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root
22+
* 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。
23+
* boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true ;否则返回 false 。
24+
* int next()将指针向右移动,然后返回指针处的数字。
25+
*
26+
*
27+
* 注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。
28+
*
29+
*
30+
*
31+
* 你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。
32+
*
33+
*
34+
*
35+
* 示例:
36+
*
37+
*
38+
* 输入
39+
* ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next",
40+
* "hasNext", "next", "hasNext"]
41+
* [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
42+
* 输出
43+
* [null, 3, 7, true, 9, true, 15, true, 20, false]
44+
*
45+
* 解释
46+
* BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
47+
* bSTIterator.next(); // 返回 3
48+
* bSTIterator.next(); // 返回 7
49+
* bSTIterator.hasNext(); // 返回 True
50+
* bSTIterator.next(); // 返回 9
51+
* bSTIterator.hasNext(); // 返回 True
52+
* bSTIterator.next(); // 返回 15
53+
* bSTIterator.hasNext(); // 返回 True
54+
* bSTIterator.next(); // 返回 20
55+
* bSTIterator.hasNext(); // 返回 False
56+
*
57+
*
58+
*
59+
*
60+
* 提示:
61+
*
62+
*
63+
* 树中节点的数目在范围 [1, 10^5] 内
64+
* 0
65+
* 最多调用 10^5 次 hasNext 和 next 操作
66+
*
67+
*
68+
*
69+
*
70+
* 进阶:
71+
*
72+
*
73+
* 你可以设计一个满足下述条件的解决方案吗?next() 和 hasNext() 操作均摊时间复杂度为 O(1) ,并使用 O(h) 内存。其中 h
74+
* 是树的高度。
75+
*
76+
*
77+
*/
78+
79+
import { TreeNode } from './commons/Tree'
80+
81+
export
82+
// @lc code=start
83+
class BSTIterator {
84+
private curr: TreeNode | null
85+
86+
private readonly stack: TreeNode[] = []
87+
88+
constructor(root: TreeNode | null) {
89+
this.curr = root
90+
}
91+
92+
next(): number {
93+
while (this.curr) {
94+
this.stack.push(this.curr)
95+
this.curr = this.curr.left
96+
}
97+
98+
this.curr = this.stack.pop()!
99+
const result = this.curr.val
100+
this.curr = this.curr.right
101+
102+
return result
103+
}
104+
105+
hasNext(): boolean {
106+
return !!(this.stack.length || this.curr)
107+
}
108+
}
109+
// @lc code=end
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* @lc app=leetcode.cn id=215 lang=typescript
3+
*
4+
* [215] 数组中的第K个最大元素
5+
*
6+
* https://leetcode.cn/problems/kth-largest-element-in-an-array/description/
7+
*
8+
* algorithms
9+
* Medium (64.65%)
10+
* Likes: 1821
11+
* Dislikes: 0
12+
* Total Accepted: 714.7K
13+
* Total Submissions: 1.1M
14+
* Testcase Example: '[3,2,1,5,6,4]\n2'
15+
*
16+
* 给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。
17+
*
18+
* 请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
19+
*
20+
* 你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
*
27+
* 输入: [3,2,1,5,6,4], k = 2
28+
* 输出: 5
29+
*
30+
*
31+
* 示例 2:
32+
*
33+
*
34+
* 输入: [3,2,3,1,2,4,5,5,6], k = 4
35+
* 输出: 4
36+
*
37+
*
38+
*
39+
* 提示:
40+
*
41+
*
42+
* 1 <= k <= nums.length <= 10^5
43+
* -10^4 <= nums[i] <= 10^4
44+
*
45+
*
46+
*/
47+
48+
export
49+
// @lc code=start
50+
function findKthLargest(nums: number[], k: number): number {
51+
quickSelect(
52+
nums,
53+
(a, b) => a - b,
54+
0,
55+
nums.length - 1,
56+
k,
57+
)
58+
59+
return nums[nums.length - k]
60+
}
61+
62+
function quickSelect<T>(
63+
arr: T[],
64+
compare: (a: T, b: T) => number,
65+
left: number,
66+
right: number,
67+
k: number,
68+
) {
69+
if (left >= right) return
70+
71+
const i = partition(arr, compare, left, right)
72+
const target = right - k + 1
73+
74+
if (i < target) {
75+
quickSelect(arr, compare, i + 1, right, k)
76+
} else if (i > target) {
77+
quickSelect(arr, compare, left, i - 1, i - target)
78+
}
79+
}
80+
81+
function partition<T>(
82+
arr: T[],
83+
compare: (a: T, b: T) => number,
84+
left: number,
85+
right: number,
86+
): number {
87+
// const randomIndex = Math.floor(Math.random() * (right - left)) + left
88+
// swap(arr, left, randomIndex)
89+
90+
const pivotValue = arr[left]
91+
92+
let j = left
93+
for (let i = left + 1; i <= right; i++) {
94+
if (compare(arr[i], pivotValue) <= 0) {
95+
swap(arr, i, ++j)
96+
}
97+
}
98+
99+
swap(arr, left, j)
100+
return j
101+
}
102+
103+
function swap(arr: any[], i: number, j: number) {
104+
const temp = arr[i]
105+
arr[i] = arr[j]
106+
arr[j] = temp
107+
}
108+
// @lc code=end

0 commit comments

Comments
 (0)