Skip to content

Commit 6fbfdd7

Browse files
committed
作业提交
1 parent 6d538f6 commit 6fbfdd7

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val, children) {
4+
* this.val = val;
5+
* this.children = children;
6+
* };
7+
*/
8+
9+
/**
10+
* @param {Node} root
11+
* @return {number[]}
12+
*/
13+
var preorder = function(root) {
14+
let nums = []
15+
let preorderFun = (node) => {
16+
if (node) {
17+
nums.push(node.val)
18+
for (var i = 0; i < node.children.length; i ++) {
19+
preorderFun(node.children[i])
20+
}
21+
}
22+
}
23+
24+
preorderFun(root)
25+
return nums
26+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 两个hashmap
3+
* @param {string} s
4+
* @param {string} t
5+
* @return {boolean}
6+
*/
7+
var isAnagram = function(s, t) {
8+
if (s.length !== t.length) {
9+
return false
10+
}
11+
var hm1 = new Map();
12+
var hm2 = new Map();
13+
for (var i = 0; i < s.length; i ++) {
14+
if (hm1.has(s[i])) {
15+
hm1.set(s[i], hm1.get(s[i]) + 1)
16+
} else {
17+
hm1.set(s[i], 1)
18+
}
19+
if (hm2.has(t[i])) {
20+
hm2.set(t[i], hm2.get(t[i]) + 1)
21+
} else {
22+
hm2.set(t[i], 1)
23+
}
24+
}
25+
for(let key of hm1.keys()) {
26+
if (!hm2.has(key) || hm2.get(key) !== hm1.get(key)) {
27+
return false
28+
}
29+
}
30+
return true
31+
};
32+
33+
/**
34+
* 计数器
35+
* @param {string} s
36+
* @param {string} t
37+
* @return {boolean}
38+
*/
39+
var isAnagram = function(s, t) {
40+
if (s.length !== t.length) {
41+
return false
42+
}
43+
let counter = new Array(26).fill(0);
44+
var aCode = 'a'.charCodeAt(0);
45+
for (var i = 0; i < s.length; i ++) {
46+
counter[s.charCodeAt(i) - aCode]++
47+
counter[t.charCodeAt(i) - aCode]--
48+
}
49+
for(var j = 0; j < counter.length; j ++) {
50+
if (counter[j] < 0) {
51+
return false
52+
}
53+
}
54+
return true
55+
};

0 commit comments

Comments
 (0)