JAVA实现lowerBound和upperBound函数

JAVA实现lowerBound和upperBound函数

//找到第一个大于等于x的位置
        public static int lowerBound(User[] user , int low, int high, int x){
                int mid;
                while (low <= high){
                        mid = (low + high)>>1;
                        if(user[mid].likeValue >= x)
                                high = mid-1;
                        else
                                low = mid+1;

                }
                return low;
        }
 //找到第一个大于x的位置
        public static int upperBound(User[] user, int low, int high, int x){
                int mid;
                while (low<high){
                        mid = (low+high)>>1;
                        if(user[mid].likeValue<=x)
                                low = mid+1;
                        else
                                high = mid;
                }
                return low;
        }
全部评论

相关推荐

程序员猪皮:看不到八股什么意思
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务