Shawn's Blog

  • Home
  • Tags
  • Categories
  • Archives
  • Search
  • Table of Contents
  • Overview

Shawn

若有恒,何必三更眠五更起;最无益,莫过一日曝十日寒。
392 posts
13 categories
114 tags

LeetCode 374. Guess Number Higher or Lower

Posted on 2021-06-14 Edited on 2024-11-24 In LeetCode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int guessNumber(int n) {
int l = 1, r = n;
while (l < r) {
int mid = (r - l) / 2 + l;
int ans = guess(mid);
if (ans == -1) r = mid - 1;
else if (ans == 1) l = mid + 1;
else return mid;
}
return r;
}
};
# Binary Search
LeetCode 1896. Minimum Cost to Change the Final Value of Expression
LeetCode 1449. Form Largest Integer With Digits That Add up to Target
© 2024 Shawn
Powered by Hexo & NexT.Mist
0%