Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

35. Search Insert Position

Posted on 2020-06-28 | Edited on 2021-01-22

LeetCode

Binary Search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
private int binarySearch(int[] nums,int target,int l,int r){
while(l<=r){
int mid=l+(r-l)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]<target) l=mid+1;
else r=mid-1;
}
return l;
}
public int searchInsert(int[] nums, int target) {
return binarySearch(nums,target,0,nums.length-1);
}
}

<1…133134135…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%