• 0,1序列中找到第一个1出现的位置


    /*
     * 从0,1数组中找到第一个1出现的位置
     * 1一旦出现,则以后全为1
     * 如:0011111
     */
    public class FindFirstOne {
        //参数:数组a,低位low,高位a.length-1
        //返回第一个1出现的位置
        //如果全是0,返回数组长度
        public static int find(int[] a,int low,int high) {
            int flag=0;
            while(high-low>1) {                  //当low,high相邻时,结束  可以知道,low永远在high的左边
                int mid=(low+high)/2;
                if(a[mid]==1) {
                    high=mid;
                }else {
                    low=mid;
                }
            }
            if(a[low]==1) return low;
            if(a[high]==1) return high;      
            return a.length;
        }
        
        public static void main(String[] args) {
            int[] a= {0,1,1,1,1};        //最好多举例,演示几遍就清楚了
            int f=find(a,0,a.length-1);  
            System.out.println(f);
        }
    }
  • 相关阅读:
    HDU 1068
    hdu6447
    HDU 6438
    网络赛的个人反思总结
    Bellman-ford 模板
    Pairs Forming LCM LightOJ
    POJ
    链式前向星
    POJ 3281 Dining
    游标遍历所有数据库循环执行修改数据库的sql命令
  • 原文地址:https://www.cnblogs.com/heyboom/p/8996820.html
Copyright © 2020-2023  润新知