日期:2014-05-20  浏览次数:21048 次

求个拆分IP地址的算法
现在有一个List<String>的IP地址
221.226.83.50
221.226.83.51
221.226.83.52
221.226.83.53

221.226.83.61
221.226.83.62
221.226.83.63
221.226.83.64
。。。。
要写一个算法:
返回成分段的List(“221.226.83.50-221.226.83.53”,“221.226.83.61-221.226.83.64”)

大虾们帮帮忙

------解决方案--------------------
Java code

public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("221.226.83.9");
        list.add("221.226.83.10");
        list.add("221.226.83.11");
        list.add("221.224.83.53");
        list.add("221.224.83.52");
        list.add("221.226.87.21");
        list.add("221.226.87.22");
        Set<String> set = new TreeSet<String>(new Comparator<String>(){
            public int compare(String arg0, String arg1){
                String[] key1 = arg0.split("\\.");
                String[] key2 = arg1.split("\\.");
                for(int i= 0;i<key1.length;i++){
                    if(Integer.parseInt(key1[i]) == Integer.parseInt(key2[i])){
                        continue;
                    }
                    if(Integer.parseInt(key1[i]) > Integer.parseInt(key2[i])){
                        return 1;
                    }
                    if(Integer.parseInt(key1[i]) < Integer.parseInt(key2[i])){
                        return -1;
                    }
                }
                return 0;
            }});
        set.addAll(list);
        System.out.println(set);
        List<String> result = new ArrayList<String>();
        String s1 = null;
        String s2 = null;
        String start = null;
        String end = null;
        int count = 0;
        for(String s : set){
            count ++;
            if(s1==null){
                s1 = s;
                start = s;
            }
            else{
                s2 = s;
                if(s2.startsWith(s1.substring(0,s1.lastIndexOf(".")))&&
                        Integer.parseInt(s2.substring(s2.lastIndexOf(".")+1))
                             - Integer.parseInt(s1.substring(s1.lastIndexOf(".")+1)) == 1){
                    end = s;
                    s1 = s2;
                }
                
                else{
                    result.add(start + (end==null?"":"-"+end));
                    s1 = s;
                    start = s;
                    end = null;
                }
                
                if(count == set.size()){
                    result.add(start+ (end==null?"":"-"+end));
                    s1=null;
                    end =null;
                }
            }
        }
        System.out.println(result);
    }

------解决方案--------------------
Java code

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test03 {
    private static List<String> IP_LIST = new ArrayList<String>();
    static {
        IP_LIST.add("221.226.83.50");
        IP_LIST.add("221.226.83.51");
        IP_LIST.add("221.226.83.52");
        IP_LIST.add("221.226.83.53");
        IP_LIST.add("221.226.83.54");
        IP_LIST.add("221.226.83.55");
        IP_LIST.add("221.226.83.61");
        IP_LIST.add("221.226.83.62");
        IP_LIST.add("221.226.83.63");
        IP_LIST.add("221.226.83.64");
        IP_LIST.add("221.226.83.75");
        IP_LIST.add("221.226.83.255");
        IP_LIST.add("221.226.84.1");
        IP_LIST.add("221.226.84.2");
        IP_LIST.add("221.226.84.3");
        IP_LIST.add("221.226.85.4");
    }

    public static void main(String[] args) {
        List<String> list = getIPList(IP_LIST);
        for (String str : list) {
            System.out.println(str);
        }
    }

    public static List<String> getIPList(List<String> ipList) {
        Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
        for (String ip : ipList) {
            int index = ip.lastIndexOf('.');
            String ip123 = ip.substring(0, index + 1);
            int ip4 = Integer.valueOf(ip.substring(index + 1, ip.length()));
            if (map.containsKey(ip123)) {
                map.get(ip123).add(ip4);
            } else {
                List<Integer> ip4List = new ArrayList<Integer>();
                ip4List.add(ip4);
                map.put(ip123, ip4List);
            }
        }

        List<String> list = new ArrayList<String>();
        for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
            String ip123 = entry.getKey();
            List<Integer> ip4List = entry.getValue();
            Collections.sort(ip4List);
            int len = ip4List.size();
            for (int i = 0; i < len; i++) {
                int start = ip4List.get(i);
                int stop = start;

                while (++i < len) {
                    int current = ip4List.get(i);
                    if (current == (stop + 1)) {
                        stop = current;
                    } else {
                        list.add(start == stop ? ip123 + String.valueOf(start)
                                : ip123 + String.valueOf(start) + "-" + ip123
                                        + String.valueOf(stop));
                        i--;
                        break;
                    }
                }

                if (i >= len) {
                    list.add(start == stop ? ip123 + String.valueOf(start)
                            : ip123 + String.valueOf(start) + "-" + ip123
                                    + String.valueOf(stop));
                }
            }
        }
        return list;
    }
}