[Leetcode]--Search for a Range

[Leetcode]--Search for a Range
public class Solution {
    public int[] searchRange(int[] A, int target) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int start = 0;
        int end = A.length - 1;
        int l = -1;
        int r = -1;
        while(start <= end){
            int mid = (start + end) / 2;
            if(A[mid] == target) l = mid;
            /*如果是target <= A[mid] 找到的是数组下标最小的 */
            if(target <= A[mid]) end = mid - 1;
            else start = mid + 1;
        }
        /*不要忘记把指针归位*/
        start = 0;
        end = A.length - 1;
        while(start <= end){
            int mid = (start + end) / 2;
            if(A[mid] == target) r = mid;
            /*如果是target <= A[mid] 找到的是数组下标最大的 */
            if(target >= A[mid]) start = mid + 1;
            else end = mid - 1;
        }
        return new int[]{l, r};
    }
}
[Leetcode]--Search for a Range

[Leetcode]--Search for a Range

上一篇:CorelDRAW X5怎么快速绘制篮球?


下一篇:利用Flash切割功能绘制圆缺的月亮