-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path编程-算法.js
30 lines (27 loc) · 890 Bytes
/
编程-算法.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 算法
// Q1: 二叉树中序遍历
// Q2: 数组的随机排序方法,要求概率一致
// Q1 celine 23.2.13
// leetcode https://leetcode.cn/problems/binary-tree-inorder-traversal/description/
function mid(root) {
if (!root) return [];
const result = [];
const traverse = (node) => {
if (node.left) traverse(node.left);
result.push(node.val);
if (node.right) traverse(node.right);
}
traverse(root);
return result;
}
// Q2 celine 23.2.13
// leetcode https://leetcode.cn/problems/shuffle-an-array/description/?orderBy=hot
Solution.prototype.shuffle = function () {
for (let i = 0; i < this.nums.length; i++) {
const target = Math.floor(Math.random() * (this.nums.length - i)) + i;
const temp = this.nums[i];
this.nums[i] = this.nums[target];
this.nums[target] = temp;
}
return this.nums
};