Created
July 1, 2019 16:14
-
-
Save Desolve/1e8ce8f5cb9f93d7735cddc4acd5fad0 to your computer and use it in GitHub Desktop.
0035 Search Insert Position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int searchInsert(int[] nums, int target) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
int i = 0, j = nums.length-1; | |
int index = -1; | |
while(i <= j) { | |
index = (i + j) / 2; | |
if(nums[index] == target) | |
return index; | |
if(nums[index] >= target) | |
j = index - 1; | |
else | |
i = index + 1; | |
} | |
return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment