题解 | #二分查找#
二分查找
https://www.nowcoder.com/practice/28d5a9b7fc0b4a078c9a6d59830fb9b9
import java.util.*; public class BinarySearch { public int getPos(int[] A, int n, int val) { int left = 0, right = n-1; while (left < right) { int mid = (left + right) / 2; if (A[mid] >= val) { right = mid; } else { left = mid + 1; } } if (A[left] == val) { return left; } else { return -1; } } }