Advertisement
asdfg0998

sol.cpp

Sep 10th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int searchInsert(vector<int>& nums, int target) {
  4. int l =0;
  5. int r = nums.size()-1;
  6. int ans = 0;
  7. while(l<=r){
  8. int m = (l+r)/2;
  9. if(target> nums[m]){
  10. l = m+1;
  11. }
  12. else if(m > 0 && target<nums[m]){
  13. r = m-1;
  14. }
  15. else{
  16. return m;
  17. }
  18. }
  19. ans = l;
  20. return ans;
  21.  
  22. }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement