-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
80 lines (69 loc) · 1.98 KB
/
Copy pathcommon.js
File metadata and controls
80 lines (69 loc) · 1.98 KB
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
/**
给一非空的单词列表,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
示例 1:
输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
输出: ["i", "love"]
解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。
注意,按字母顺序 "i" 在 "love" 之前。
链接:https://leetcode-cn.com/problems/top-k-frequent-words
*/
/**
*
* @param {Array} words 需要比较的数组
* @param {*} k 筛选出的元素个数
* @returns
*/
var topKFrequent = function(words, k) {
let map = new Map()
let arr = words.sort()
arr.forEach(item => {
if(map.has(item)) {
let count = map.get(item)
map.set(item, ++count)
} else {
map.set(item, 1)
}
})
let ent = [...map.entries()].sort((a, b) => {
return b[1] - a[1]
})
let result = []
for(let i = 0; i < k; i++) {
result.push(ent[i][0])
}
return result
};
/**
* 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
* 示例 1:
* 输入: s = "abcabcbb"
* 输出: 3
* 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
*/
// 除了暴力 循环的思路之外,看了题解才做出来的一道题 !!!!!!!!!!!!!!!!!!!!!!!!!!!!
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let codeSet = new Set()
let n = s.length
let ans = 0
let end = 0
for(let i = 0; i < n; i++) {
if(i) {
codeSet.delete(s.charAt(i -1))
}
while(end < n && !codeSet.has(s.charAt(end))) {
codeSet.add(s.charAt(end))
end++
}
ans = Math.max(ans, end - i)
}
return ans
};