Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created July 1, 2019 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/1e8ce8f5cb9f93d7735cddc4acd5fad0 to your computer and use it in GitHub Desktop.
Save Desolve/1e8ce8f5cb9f93d7735cddc4acd5fad0 to your computer and use it in GitHub Desktop.
0035 Search Insert Position
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