add: 最高频率的IP
This commit is contained in:
parent
57045d7772
commit
34a76e22b9
@ -303,6 +303,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 152. 乘积最大子数组 <https://leetcode-cn.com/problems/maximum-product-subarray/>
|
||||
- LintCode 191. 乘积最大子序列 <https://www.lintcode.com/problem/maximum-product-subarray/description>
|
||||
|
||||
- [最高频率的IP](src/array/highest-frequency-ip.js)
|
||||
|
||||
- LintCode 1613. 最高频率的IP <https://www.lintcode.com/problem/highest-frequency-ip/description>
|
||||
|
||||
## 栈
|
||||
|
||||
- [最大矩阵](src/stack/maximal-rectangle.js)
|
||||
|
19
src/array/highest-frequency-ip.js
Normal file
19
src/array/highest-frequency-ip.js
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @param ipLines: ip address
|
||||
* @return: return highestFrequency ip address
|
||||
*/
|
||||
export const highestFrequency = function (ipLines) {
|
||||
const map = new Map()
|
||||
let max = 0
|
||||
let maxN = ipLines[0]
|
||||
ipLines.forEach(n => {
|
||||
const temp = (map.get(n) || 0) + 1
|
||||
map.set(n, temp)
|
||||
if (temp > max) {
|
||||
maxN = n
|
||||
max = temp
|
||||
}
|
||||
})
|
||||
|
||||
return maxN
|
||||
}
|
6
test/array/highest-frequency-ip.test.js
Normal file
6
test/array/highest-frequency-ip.test.js
Normal file
@ -0,0 +1,6 @@
|
||||
import { highestFrequency } from '../../src/array/highest-frequency-ip'
|
||||
|
||||
test('最高频率的IP', () => {
|
||||
expect(highestFrequency(['192.168.1.1', '192.118.2.1', '192.168.1.1'])).toEqual('192.168.1.1')
|
||||
expect(highestFrequency(['192.168.1.1', '192.118.2.1', '192.168.1.1', '192.118.2.1', '192.118.2.1'])).toEqual('192.118.2.1')
|
||||
})
|
Loading…
Reference in New Issue
Block a user