最近同学接到阿里面试题

 package io.guangsoft.analysis;

 /*
数据文件:
1.1.1.0/24,123
1.1.2.0/28,345
1.2.0.0/16,789
*/
public interface Finder {
//完成初始化动作
boolean loadFile(String fileName);
//查找具体IP所在IP网段的键值,未找到返回-1;比如如果输入样例数据文件,调用find("1,1,1,1"),返回123
int find(String ipStr);
}

搜索一番解答如下

 package io.guangsoft.analysis;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.net.util.SubnetUtils;
import org.apache.commons.net.util.SubnetUtils.SubnetInfo; public class FinderImpl implements Finder { private Map<String, String> dataMap = new HashMap<String, String>(); @Override
public boolean loadFile(String fileName) {
File file = new File(this.getClass().getClassLoader().getResource(fileName).getFile());
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while(true) {
String nextLine = bufferedReader.readLine();
if(nextLine == null) {
break;
} else {
String str[] = nextLine.split(",");
dataMap.put(str[0], str[1]);
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
} @Override
public int find(String ip) {
for(String network : dataMap.keySet()) {
boolean bingo = isInRange(ip, network);
if(bingo) {
return Integer.parseInt(dataMap.get(network));
}
}
return -1;
} //核心代码,检索IP所属网段
public boolean isInRange(String ip, String network) {
String[] ips = ip.split("\\.");
int ipAddr = (Integer.parseInt(ips[0]) << 24)
| (Integer.parseInt(ips[1]) << 16)
| (Integer.parseInt(ips[2]) << 8)
| Integer.parseInt(ips[3]);
int type = Integer.parseInt(network.replaceAll(".*/", ""));
int mask = 0xFFFFFFFF << (32 - type);
String networkIp = network.replaceAll("/.*", "");
String[] networkIps = networkIp.split("\\.");
int networkIpAddr = (Integer.parseInt(networkIps[0]) << 24)
| (Integer.parseInt(networkIps[1]) << 16)
| (Integer.parseInt(networkIps[2]) << 8)
| Integer.parseInt(networkIps[3]);
return (ipAddr & mask) == (networkIpAddr & mask);
} //也可以使用apache的net工具类
public boolean isInRange2(String ip, String network) {
SubnetInfo subnetInfo = new SubnetUtils(network).getInfo();
return subnetInfo.isInRange(ip);
} public static void main(String args[]) {
FinderImpl finder = new FinderImpl();
if(finder.loadFile("data.txt")) {
int num = finder.find("1.1.1.1");
System.out.println(num);
}
} }

最新文章

  1. 前端MVVM框架avalon揭秘 - 双向绑定原理
  2. Record:Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView
  3. log4net--帮助程序员将日志信息输出到各种目标(控制台、文件、数据库等)的工具
  4. SpringBoot入门 一 构建简单工程
  5. Cacti优化之spine轮询器
  6. Windows Server 2012 安装dll到GAC
  7. Hibernate 框架基本知识
  8. android4.0下如何判断手机是否有底部物理按键(menu物理按键)
  9. AsParallel 用法
  10. Babel初体验
  11. 组合 Lucas定理
  12. This version of the rendering library is more recent than your version of ADT plug-in. Please update
  13. Ubuntu系统下配置IP地址方法介绍
  14. adb devices 报错处理
  15. mysql导出导入数据
  16. 基于Spring Boot和Shiro的后台管理系统FEBS
  17. Windows 服务安装教程
  18. java痛苦学习之路[十]--日常问题汇总
  19. 详解Object.constructor
  20. pytest文档25-conftest.py作用范围

热门文章

  1. css位置相关元素
  2. c++11 类型推断
  3. spring的@Transactional注解详细用法(转载)
  4. HDU 5701 中位数计数 百度之星初赛
  5. C#中命名空间别名的使用
  6. Django 翻译与 LANGUAGE_CODE
  7. 透明 Transparent connections through HTTP proxies.
  8. windbg遍历进程页表查看内存
  9. 你应该知道的vim插件之surround.vim
  10. Redis for Python开发手册