中位数
int max = Integer.MAX_VALUE-1;int b =(max+1)>>>1;// 1073741823int a =(max+1)/2;// 1073741823int c =1+(max-1)/2;// 1073741823int d = max-(max-1)/2;// 1073741824int max = Integer.MAX_VALUE;int b =(max+1)>>>1;// 1073741824int a =(max+1)/2;// -1073741824int c =1+(max-1)/2;// 1073741824int d = max-(max-1)/2;// 1073741824
JDK 中用法: mid = (l+r)>>>1;
l + r在发生整型溢出以后,会变成负数,此时如果除以 2 ,mid 是一个负数,但是经过无符号右移>>>,可以得到在不溢出的情况下正确的结果。
二分查找二分查找细节详解转变数组后最接近目标值的数组和取中位数表达式写法 mid = (l+r)>>>1; 中位数 int max = Integer.MAX_VALUE-1; int b = (max+1)>>>1; int a = (max+1)/2; int c = 1+(max-1)/2; int d = max-(max-1)/2; System.out