-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
198 lines (170 loc) · 4.35 KB
/
utils.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import React from "react";
import { Button, message } from "antd";
import { ISSUES_URL } from "./constant/index";
function TreeNode(value) {
return {
left: null,
right: null,
value,
};
}
const seen = {};
export function getRandomUnique(lower, upper, amount) {
console.log(seen);
// [10, 20]10
const start = getRandom(0, upper - lower - amount);
if (`${lower}-${upper}` in seen)
return seen[`${lower}-${upper}`].slice(start, start + amount + 1);
const condidates = Array.from(Array(upper - lower + 1), (_, i) => i + lower);
const n = condidates.length;
for (let i = n - 1; i >= 0; i--) {
const temp = condidates[i];
const number = (Math.random() * n) >>> 0;
condidates[i] = condidates[number];
condidates[number] = temp;
}
seen[`${lower}-${upper}`] = condidates;
return condidates.slice(start, start + amount + 1);
}
export function getRandom(n, m) {
return Math.round(Math.random() * (m - n) + n);
}
export function copyToClipboard(str) {
try {
const el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
return true;
} catch (err) {
return false;
}
}
export function serialise_bfs(root) {
let ans = "[";
const q = [root];
let cur = null;
while (q.length > 0) {
cur = q.shift();
if (cur) {
q.push(cur.left);
q.push(cur.right);
ans += cur.value + ",";
} else {
ans += "null,";
}
}
return ans.slice(0, -1) + "]";
}
export function buildRandomTree({
amount = 10,
upper = 10,
lower = 0,
isUnique,
}) {
let remain = amount;
let condidates = [];
if (isUnique) {
condidates = getRandomUnique(lower, upper, amount);
}
function dfs({ upper, lower }) {
if (remain <= 0) return null;
remain -= 1;
const root = TreeNode(
!isUnique ? getRandom(lower, upper) : condidates[remain]
);
if (Math.random() > 0.5) {
root.left = dfs({ upper, lower });
}
if (Math.random() > 0.5) {
root.right = dfs({ upper, lower });
}
return root;
}
return dfs({ upper, lower });
}
export function 不讲武德() {
return message.error({
content: (
<>
力扣不讲武德,不按套路出牌。不过没关系啊,你
<Button type="link" href={ISSUES_URL} target="_blank">
反馈
</Button>
给我,下次一定全部���出去!
</>
),
});
}
export function uuidv4() {
return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
export function deserialise_bfs(nodes) {
if (nodes.length === 0 || nodes[0] === "null") return null;
const root = TreeNode(nodes[0]);
const q = [root];
let i = 0;
while (q.length > 0 && i < nodes.length - 1) {
const cur = q.shift();
const l = TreeNode(nodes[i + 1]);
if (l.value !== "null") {
q.push(l);
cur.left = l;
}
if (i < nodes.length - 2) {
const r = TreeNode(nodes[i + 2]);
if (r.value !== "null") {
q.push(r);
cur.right = r;
}
}
i += 2;
}
return {
root,
};
}
export function copy(text, cb) {
//Create a textbox field where we can insert text to.
var copyFrom = document.createElement("textarea");
//Set the text content to be the text you wished to copy.
copyFrom.textContent = text;
//Append the textbox field into the body as a child.
//"execCommand()" only works when there exists selected text, and the text is inside
//document.body (meaning the text is part of a valid rendered HTML element).
document.body.appendChild(copyFrom);
//Select all the text!
copyFrom.select();
//Execute command
document.execCommand("copy");
//(Optional) De-select the text using blur().
copyFrom.blur();
//Remove the textbox field from the document.body, so no other JavaScript nor
//other elements can get access to this.
document.body.removeChild(copyFrom);
if (cb instanceof Function) cb();
}
const COLORS = [
"magenta",
"red",
"volcano",
"orange",
"gold",
"lime",
"green",
"cyan",
"blue",
"geekblue",
"purple",
];
export function getColor(text) {
let ans = 0;
for (const c of text) ans += c.charCodeAt();
return COLORS[ans % COLORS.length];
}