Java solution that beats 100%


  • 0

    This solution seems to have worked for me, and according to the judge it beats 100% of Java solutions.

    public int searchInsert(int[] nums, int target) {
            int low = 0;
            int high = nums.length - 1;
            int mid = low + (high - low) / 2;
            while (low <= high) {
                if (target < nums[mid]) {
                    high = mid - 1;
                }
                else if (target > nums[mid]) {
                    low = mid + 1;
                }
                else {
                    return mid;
                }
                mid = low + (high - low) / 2;
            }
            return mid;
        }
    

Log in to reply
 

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