Skip to content

Commit

Permalink
이슈 #133에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 24, 2024
1 parent ee232d7 commit afd1145
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions LeetCode/Guess_Number_Higher_or_Lower.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* int guess(int num);
*/

class Solution {
public:
int guessNumber(int n) {
int left = 1;
int right = n;

while (left <= right) {
int mid = left + (right - left) / 2;
int res = guess(mid);
if (res == 0) {
return mid;
} else if (res == 1) {
left = mid + 1;
} else if (res == -1) {
right = mid - 1;
}
}

return -1;
}
};

0 comments on commit afd1145

Please sign in to comment.