| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 459fc66 commit 065aaa0
3 files changed
| Original file line number | Diff line number | Diff 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 | + }; | ||
| Original file line number | Diff line number | Diff 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 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1 +1,13 @@ | |||
| 1 | - # 学习笔记 | ||
| 1 | + # 学习笔记 | ||
| 2 | + | ||
| 3 | + #### 散列表 | ||
| 4 | + | ||
| 5 | + - 查询时间复杂度 O(1),所以主要用于计数,需要配合排序及其他算法使用 | ||
| 6 | + | ||
| 7 | + #### 跳表 | ||
| 8 | + | ||
| 9 | + - 利用多级索引,提高查找效率 | ||
| 10 | + | ||
| 11 | + #### 二叉树 | ||
| 12 | + | ||
| 13 | + - 递归算法 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments