FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

finish homework of second week · feixiangcode/algorithm@065aaa0 · GitHub

Commit 065aaa0

Browse files
jiawenhui
committed
finish homework of second week
1 parent 459fc66 commit 065aaa0

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎Week_02/id_130/LeetCode_242_130.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/valid-anagram/
2+
3+
/**
4+
* @param {string} s
5+
* @param {string} t
6+
* @return {boolean}
7+
*/
8+
var isAnagram = function(s, t) {
9+
if (s.length !== t.length) return false;
10+
const countS = new Array(26).fill(0);
11+
const countT = new Array(26).fill(0);
12+
for (let i = 0; i < s.length; i++) {
13+
countS[s.charCodeAt(i) - 97]++;
14+
countT[t.charCodeAt(i) - 97]++;
15+
}
16+
17+
for (let i = 0; i < 26; i++) {
18+
if (countS[i] !== countT[i]) {
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
};

‎Week_02/id_130/LeetCode_692_130.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/top-k-frequent-words/
2+
3+
/**
4+
* @param {string[]} words
5+
* @param {number} k
6+
* @return {string[]}
7+
*/
8+
var topKFrequent = function(words, k) {
9+
const counts = {};
10+
for(let i = 0; i < words.length; i++) {
11+
counts[words[i]] ? counts[words[i]]++ : counts[words[i]] = 1;
12+
}
13+
14+
const keys = Object.keys(counts).sort((a, b) => {
15+
if (counts[a] === counts[b]) {
16+
if (a > b) {
17+
return 1;
18+
} else {
19+
return -1;
20+
}
21+
}
22+
else {
23+
return counts[b] - counts[a];
24+
}
25+
})
26+
.slice(0, k);
27+
28+
return keys.slice(0, k);
29+
};

‎Week_02/id_130/NOTE.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
# 学习笔记
1+
# 学习笔记
2+
3+
#### 散列表
4+
5+
- 查询时间复杂度 O(1),所以主要用于计数,需要配合排序及其他算法使用
6+
7+
#### 跳表
8+
9+
- 利用多级索引,提高查找效率
10+
11+
#### 二叉树
12+
13+
- 递归算法

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL