278. First Bad Version-LeetCode(查找第一个最坏版本)

/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int first=1;
        int last=n;
        int mid=(first+last)/2;            

        int temp=getPosition(mid);

        while(true){
            switch(temp){
                case 0:
                    return n;
                    //break;
                case 1:
                    last=mid-1;
                    mid=(first+last)/2;
                    temp=getPosition(mid);
                    break;
                case -1:
                    first=mid+1;
                    mid=(first+last)/2;
                    temp=getPosition(mid);
                    break;
            }

        }
    }

    private int getPosition(int n){//-1 means before ,0 means is,1 means after
        if(isBadVersion(n)==false){
            return -1;
        }else if(n==1){
            return 0;
        }else if(isBadVersion(n-1)==true){
            return 1;
        }else{
            return 0;
        }
    }
    /*public boolean isFirst(int n){
        if(isBadVersion(n)==true){
                 if(n==1){
                return true;
            }else if(isBadVersion(n-1)==false){
                return true;
            }
        }
        return false;

    }
    public boolean isBefore(int n ){
        if(isBadVersion(n)==false){
            return true;
        }
        return false;
    }
    public boolean isAfter(int n){
        if(isBadVersion(n)==true&&isBadVersion(n-1)==true){
            return true;
        }
        return false;
    }*/

}

第二套方案

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int first=1;
        int last=n;
        int mid=(first+last)/2;            

        //int temp=getPosition(mid);
        if(isBadVersion(1)==true){
            return 1;
        }
        while(true){
            if(first+1==last){
                return last;
            }

            if(isBadVersion(mid)==true){
                last=mid;
                mid=(first+last)/2;
            }else{
                first=mid;
                mid=(first+last)/2;
            }


        }
    }

最后通过了的版本

Solution{
    public int firstBadVersion(int n) {
        int first=1;
        int last=n;
        int mid;
        //int mid=(first+last)/2;            

        //int temp=getPosition(mid);
        if(isBadVersion(1)==true){
            return 1;
        }

        while(true){
            if(first+1==last){
                return last;
            }
            mid=first+(last-first)/2;
            if(isBadVersion(mid)==true){
                last=mid;
               // mid=(first+last)/2;
            }else{
                first=mid;

            }


        }
    }
}

重点来了

求一个两个数的中值的语句

  • mid=(first+last)/2;(超时)

  • mid=first+(last-first)/2;(通过)

从直观上来看前者比后者甚至少了一个加法的运算。但是又是什么原因呢?
傻乎乎地在百度知道和知乎上面问了一下,因为前者加法使得整形溢出。效果就是加法反而使得内存中的数仅剩下溢出以后剩余的数了。使得永远达不到循环结束条件。
2^32=4294967296.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小马工匠坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值