FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_118/leetcode_704_118.java at master · algorithm001/algorithm · GitHub
algorithm001
/
algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
148
Star
118
Code
Issues
548
Pull requests
46
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_704_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (30 loc) · 856 Bytes
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_704_118.java
Copy path
File metadata and controls
34 lines (30 loc) · 856 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* https://leetcode-cn.com/problems/binary-search/submissions/
*/
class
Solution
{
public
int
search
(
int
[]
nums
,
int
target
) {
// 递归解法
// 1. 边界处理:空数组
if
(
nums
.
length
<
1
){
return
-
1
;
}
// 2. 定义左右指针
int
left
=
0
;
int
right
=
nums
.
length
-
1
;
// 3. 搜索。
return
search
(
nums
,
target
,
left
,
right
);
}
int
search
(
int
[]
nums
,
int
target
,
int
left
,
int
right
){
if
(
left
>
right
){
return
-
1
;
}
int
middle
= (
right
+
left
)/
2
;
if
(
target
>
nums
[
middle
]){
return
search
(
nums
,
target
,
middle
+
1
,
right
);
}
else
if
(
target
<
nums
[
middle
]){
return
search
(
nums
,
target
,
left
,
middle
-
1
);
}
else
{
return
middle
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL