Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9febd2e

Browse files
guess number higher or lower
1 parent7566e15 commit9febd2e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packageeasy;
2+
3+
publicclassGuessNumberHigherorLower {
4+
/**The core problem/trouble to solve this problem is to figure out the problem description:
5+
* this API: guess(int num) means to take your guess num and let you know if your guessed num is bigger or smaller than the answer.
6+
* That's why if num > target, it returns -1 which means the target is smaller than your guess!!!*/
7+
8+
publicintguessNumber(intn) {
9+
intleft =1,right =n;
10+
while(left+1 <right){
11+
intmid =left + (right-left)/2;
12+
intg =guess(mid);
13+
if(g ==0)returnmid;
14+
elseif(g >0)left =mid;
15+
elseright =mid;
16+
}
17+
if(guess(left) ==0)returnleft;
18+
returnright;
19+
}
20+
21+
/**This is a fake guess method that I wrote just to compile/test, I'll have to change it to another number other than 6 based on the number to be found.*/
22+
privateintguess(intnum) {
23+
if(num >6){
24+
return -1;
25+
}elseif(num <6){
26+
return1;
27+
}else {
28+
return0;
29+
}
30+
}
31+
32+
publicstaticvoidmain(String...strings){
33+
GuessNumberHigherorLowertest =newGuessNumberHigherorLower();
34+
System.out.println(test.guessNumber(10));
35+
}
36+
37+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/)|[Solution](../../blob/master/MEDIUM/src/medium/LongestAbsoluteFilePath.java)| O(n)|O(d) | Medium| Stack
1313
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)|[Solution](../../blob/master/EASY/src/easy/FirstUniqueCharacterinaString.java)| O(n)|O(n) | Easy| HashMap
1414
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/)|[Solution](../../blob/master/MEDIUM/src/medium/LexicographicalNumbers.java)| O(n)|O(1)| Medium|
15+
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../../blob/master/EASY/src/easy/GuessNumberHigherorLower.java)| O(logn)|O(1) | Easy| Binary Search
1516
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
1617
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
1718
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp