首页 > 其他 > 详细

1613. 最高频率的IP

时间:2019-04-02 23:02:07      阅读:163      评论:0      收藏:0      [点我收藏+]

Description

Given a string[] lines, each line will have an ip address , find the ip address with the highest frequency.

The given data only has one IP with the highest frequency

Example

lines = ["192.168.1.1","192.118.2.1","192.168.1.1"]
return  "192.168.1.1"
public class Solution {
    /**
     * @param ipLines: ip  address
     * @return: return highestFrequency ip address
     */
    public String highestFrequency(String[] ipLines) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        String ans = "";
        int count = 0;
        
        for (String str: ipLines) {
            Integer occur = map.get(str);
            if (occur != null) {
                occur += 1;
            } else {
                occur = 1;
            }
            
            map.put(str, occur);
            
            if (occur >  count) {
                count = occur;
                ans = str;
            }
        }
        return ans;
    }
}
描述
给定一个字符串数组lines, 每一个元素代表一个IP地址,找到出现频率最高的IP。

给定数据只有一个频率最高的IP

您在真实的面试中是否遇到过这个题?  
样例
lines = ["192.168.1.1","192.118.2.1","192.168.1.1"]
return  "192.168.1.1"

1613. 最高频率的IP

原文:https://www.cnblogs.com/browselife/p/10646008.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!