add: 最高频率的IP

This commit is contained in:
yi-ge 2020-05-19 05:59:30 +08:00
parent 57045d7772
commit 34a76e22b9
3 changed files with 29 additions and 0 deletions

View File

@ -303,6 +303,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
- LeetCode 152. 乘积最大子数组 <https://leetcode-cn.com/problems/maximum-product-subarray/> - LeetCode 152. 乘积最大子数组 <https://leetcode-cn.com/problems/maximum-product-subarray/>
- LintCode 191. 乘积最大子序列 <https://www.lintcode.com/problem/maximum-product-subarray/description> - 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) - [最大矩阵](src/stack/maximal-rectangle.js)

View 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
}

View 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')
})