smilevchy's blog

Life & Study & Chasing

Leetcode_Search Insert Position

原题链接: https://oj.leetcode.com/problems/search-insert-position/

public int searchInsert(int[] A, int target) {
    if (A == null || A.length == 0) return 0;

    int length = A.length;
    int low = 0;
    int high = length - 1;
    int mid = 0;

    while (low <= high) {
        mid = (low + high) / 2;

        if (A[mid] == target) {
            return mid;
        } else if (A[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }

    return low;
}

algorithm

« Leetcode_ZigZag Conversion Leetcode_Maximum Subarray »