zoukankan      html  css  js  c++  java
  • 连续区间的二分查找

    连续区间的二分查找

    有连续区间 [100,200),[200,300).[300,400)...

    转为序列:100,200,300,400...

    序列中的100 代表区间[100,200)  ...以此类推

    找出数字 311在那个区间

    代码如下:

    //二分查找测试Demo
        void test003() {
            //奇数,偶数,一个数
            LinkedList<Long> tmpList=new LinkedList<Long>();
            tmpList.add(100l);
            tmpList.add(200l);
            tmpList.add(300l);
            tmpList.add(400l);
            System.out.println(binSearch(300l, tmpList, 0,tmpList.size()));
        }
        //集合
        Long binSearch(Long ip,LinkedList<Long> list,int startIndex,int endIndex){
    //        int s=startIndex,e=endIndex;
            int i=(startIndex+endIndex)/2;
            if(i==0) {//第一个(共一个)
                return list.get(0);
            }else if(i==list.size()-1){//最后一个
                return list.get(i);
            }else {
                if(ip>=list.get(i)&&ip<list.get(i+1)) {
                    return list.get(i) ;
                }else if(ip>=list.get(i+1)){
                    return binSearch(ip,list,i,endIndex);
                }else if(ip<list.get(i)){
                    return binSearch(ip,list,startIndex,i);
                }
            }
            return -1l;
        }
        //数组
        Long binSearch(Long ip,Long[] list,int startIndex,int endIndex){
    //        int s=startIndex,e=endIndex;
            int i=(startIndex+endIndex)/2;
            if(i==0) {//第一个(共一个)
                return list[0];
            }else if(i==list.length-1){//最后一个
                return list[i];
            }else {
                if(ip>=list[i]&&ip<list[i+1]) {
                    return list[i] ;
                }else if(ip>=list[i+1]){
                    return binSearch(ip,list,i,endIndex);
                }else if(ip<list[i]){
                    return binSearch(ip,list,startIndex,i);
                }
            }
            return -1l;
        }

    应用场景:比如 ip查询

    ......
    61.243.100.198 61.243.100.211 陕西省咸阳市 联通 61.243.100.212 61.243.100.212 陕西省咸阳市 联通网苑 61.243.100.213 61.243.100.214 陕西省咸阳市 亿阳网络俱乐部 61.243.100.215 61.243.100.242 陕西省咸阳市 联通 61.243.100.243 61.243.100.243 陕西省咸阳市 中医学院附属医院 61.243.100.244 61.243.100.251 陕西省咸阳市 联通 61.243.100.252 61.243.100.252 陕西省咸阳市三原县 宴友思大街1号陕西省泾惠渠管理局办公楼 61.243.100.253 61.243.101.255 陕西省咸阳市 联通 61.243.102.0 61.243.102.9 陕西省宝鸡市 联通 61.243.102.10 61.243.102.10 陕西省宝鸡市 星乐网吧 61.243.102.11 61.243.102.11 陕西省宝鸡市 小世界网吧 61.243.102.12 61.243.102.12 陕西省宝鸡市 经典网吧 61.243.102.13 61.243.102.13 陕西省宝鸡市 白云网吧 61.243.102.14 61.243.102.14 陕西省宝鸡市 聚友网吧 61.243.102.15 61.243.102.17 陕西省宝鸡市 联通 61.243.102.18 61.243.102.18 陕西省宝鸡市 畅想网吧 61.243.102.19 61.243.102.33 陕西省宝鸡市 联通 61.243.102.34 61.243.102.34 陕西省宝鸡市 世纪网吧 61.243.102.35 61.243.102.35 陕西省宝鸡市 联通 61.243.102.36 61.243.102.36 陕西省 自强中专学校 61.243.102.37 61.243.102.37 陕西省宝鸡市 联通 61.243.102.38 61.243.102.38 陕西省宝鸡市 网中网网吧 61.243.102.39 61.243.102.82 陕西省宝鸡市 联通 61.243.102.83 61.243.102.83 陕西省宝鸡市 超人网吧 61.243.102.84 61.243.102.209 陕西省宝鸡市 联通 61.243.102.210 61.243.102.210 陕西省宝鸡市 师范学院.微机室
    .......
  • 相关阅读:
    .net里面实现javascript中的 escape 和 unescape 功能
    CMM/CMMI的5个等级
    数据库连接字符串大全
    关于ExtJS的许可协议
    C#网页自动登录和提交POST信息的多种方法
    [转]如何在网页中设置禁止查看源文件
    应用程序池的配置
    解决为应用程序池 提供服务的进程关闭时间超过了限制
    百度推出开放平台 或颠覆下载网站等六大行业
    UNION会自动删除重复项,union与union all的差异
  • 原文地址:https://www.cnblogs.com/yanghaolie/p/7505302.html
Copyright © 2011-2022 走看看