FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_108/LeetCode_33_108.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
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_108
/
LeetCode_33_108.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
75 lines (70 loc) · 2.03 KB
Breadcrumbs
algorithm
/
Week_01
/
id_108
/
LeetCode_33_108.java
Copy path
File metadata and controls
75 lines (70 loc) · 2.03 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @author zhangruihao.zhang
* @version v1.0.0
* @since 2019/04/29
*/
public
class
LeetCode_33_108
{
class
Solution
{
public
int
search
(
int
[]
nums
,
int
target
) {
if
(
nums
==
null
||
nums
.
length
==
0
){
return
-
1
;
}
if
(
nums
[
0
] ==
target
){
return
0
;
}
if
(
nums
[
nums
.
length
-
1
] ==
target
){
return
nums
.
length
-
1
;
}
int
end
=
nums
.
length
-
1
;
int
min
=
min
(
nums
);
//如果没有旋转,并且最大的数小于target
if
(
min
==
0
&&
nums
[
end
]<
target
){
return
-
1
;
}
if
(
nums
[
end
] >
target
){
//target处于旋转的后半段
return
binarySerach
(
nums
,
min
,
end
,
target
);
}
else
{
//target有可能处于旋转的前半段
return
binarySerach
(
nums
,
0
,
min
-
1
,
target
);
}
}
//查找旋转点
private
int
min
(
int
[]
nums
){
int
low
=
0
;
int
high
=
nums
.
length
-
1
;
int
mid
;
while
(
low
<
high
){
mid
=
low
+ ((
high
-
low
)>>
1
);
if
(
nums
[
mid
]>
nums
[
high
]){
low
=
mid
+
1
;
}
else
{
high
=
mid
;
}
}
return
low
;
}
//二分查找
private
int
binarySerach
(
int
[]
nums
,
int
left
,
int
right
,
int
target
){
int
mid
=
0
;
while
(
left
<
right
){
mid
=
left
+ ((
right
-
left
)>>
1
);
if
(
nums
[
mid
]==
target
){
return
mid
;
}
if
(
nums
[
mid
]<
target
){
left
=
mid
+
1
;
continue
;
}
if
(
nums
[
mid
]>
target
){
right
=
mid
-
1
;
continue
;
}
}
if
(
nums
[
left
] ==
target
){
return
left
;
}
return
-
1
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL