题解 |C#二分法 旋转数组的最小数字

旋转数组的最小数字

http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba

    public int minNumberInRotateArray (List<int> rotateArray) {
        int low = 0;
        int hight = rotateArray.Count-1;
        int mid = 0;
        if(rotateArray[low] < rotateArray[hight])return rotateArray[low];
        while(low<hight)
        {          
            mid = (hight+low)/2; //每次算一次中点
            //中间数>右边数,答案区间[mid+1,hight]
            if(rotateArray[mid]>rotateArray[hight])
            {
                low = mid+1;                
            }
            //中间数<右边数,答案区间[low,mid]
            if(rotateArray[mid]<rotateArray[hight])
            {
                hight = mid;
            }
            //除上面之外的情况,左右都可能有最小数
            if(rotateArray[mid]==rotateArray[hight])
            {
                //直接比较low跟hight哪个大,大的舍弃,小的留下
                //一样的话去掉low跟hight都行(我选择的是去掉low,即让low++)
                switch(rotateArray[low]>=rotateArray[hight])
                {
                    case true:
                        low++;
                        break;
                    case false:
                        hight--; 
                        break;
                }
            }
        }
        return rotateArray[hight];      
    }
全部评论

相关推荐

2025-12-16 17:17
门头沟学院 产品经理
烤点老白薯:他第二句话的潜台词是想让你帮他点个瑞幸或者喜茶啥的
mt对你说过最有启发的一...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务