FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_118/leetcode_33_118.java at master · cloudrib/algorithm · GitHub
cloudrib
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_33_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (38 loc) · 1.08 KB
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_33_118.java
Copy path
File metadata and controls
44 lines (38 loc) · 1.08 KB
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
35
36
37
38
39
40
41
42
43
44
/**
* https://leetcode-cn.com/problems/search-in-rotated-sorted-array/
*/
class
Solution
{
public
int
search
(
int
[]
A
,
int
target
) {
// 非递归
// 边界处理
if
(
A
.
length
<
1
){
return
-
1
;
}
// 定义左右索引
int
left
=
0
;
int
right
=
A
.
length
-
1
;
// 折半搜索。需要特别注意,用 >= 还是 >,用 left=mid 还是 left=mid+1。十分重要!!!
while
(
left
<=
right
){
int
mid
= (
left
+
right
)/
2
;
if
(
A
[
mid
] ==
target
){
return
mid
;
}
if
(
A
[
mid
] <
A
[
right
]){
if
(
target
>
A
[
mid
] &&
target
<=
A
[
right
]){
left
=
mid
+
1
;
}
else
{
right
=
mid
-
1
;
}
}
else
{
if
(
target
>=
A
[
left
] &&
target
<
A
[
mid
]){
right
=
mid
-
1
;
}
else
{
left
=
mid
+
1
;
}
}
}
// 没找到
return
-
1
;
}
}
Back
|
FazBrowse Home
|
New Git URL