Simple and strict O(logn) solution in Java using recursion


  • 36
    X
    public class Solution {
        public int[] searchRange(int[] A, int target) {
            int[] range = {A.length, -1};
            searchRange(A, target, 0, A.length - 1, range);
            if (range[0] > range[1]) range[0] = -1; 
            return range;
        }
        
        public void searchRange(int[] A, int target, int left, int right, int[] range) {
            if (left > right) return;
            int mid = left + (right - left) / 2;
            if (A[mid] == target) {
                if (mid < range[0]) {
                    range[0] = mid;
                    searchRange(A, target, left, mid - 1, range);
                }
                if (mid > range[1]) {
                    range[1] = mid;
                    searchRange(A, target, mid + 1, right, range);
                }
            } else if (A[mid] < target) {
                searchRange(A, target, mid + 1, right, range);
            } else {
                searchRange(A, target, left, mid - 1, range);
            }
        }
    }

  • 0
    A

    good one! simple to understand


  • 0
    L

    concise and simple!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.