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

Commit63dd8d0

Browse files
committed
array
1 parent127db30 commit63dd8d0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎array/SearchInRotatedSortedArray.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packageAlgorithms.array;
2+
3+
publicclassSearchInRotatedSortedArray {
4+
publicintsearch(int[]A,inttarget) {
5+
if (A ==null ||A.length ==0) {
6+
return -1;
7+
}
8+
9+
intleft =0;
10+
intright =A.length -1;
11+
12+
while (left <=right) {
13+
intmid =left + (right -left) /2;
14+
if (A[mid] ==target) {
15+
returnmid;
16+
}
17+
18+
// the left side is sorted.
19+
if (A[mid] >=A[left]) {
20+
if (target <=A[mid] &&target >=A[left]) {
21+
// target is in the left side.
22+
right =mid -1;
23+
}else {
24+
// target is in the right side.
25+
left =mid +1;
26+
}
27+
}else {// the right side is sorted.
28+
if (target >=A[mid] &&target <=A[right]) {
29+
// target is in the right side.
30+
left =mid +1;
31+
}else {
32+
// target is in the right side.
33+
right =mid -1;
34+
}
35+
}
36+
}
37+
38+
return -1;
39+
}
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp