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

Commit368b74e

Browse files
author
applewjg
committed
Search in Rotated Sorted Array
Change-Id: I593d375bd9c6c383e51595186daa86c09df2c275
1 parentc4b11fb commit368b74e

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

‎SearchinRotatedSortedArray.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Search in Rotated Sorted Array
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/search-in-rotated-sorted-array/
7+
Notes:
8+
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
9+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
10+
You are given a target value to search. If found in the array return its index, otherwise return -1.
11+
You may assume no duplicate exists in the array.
12+
13+
Solution: Binary search. O(lgn) eg. [4 5 6] -7- 8 1 2, 5 6 0 -1- [2 3 4]
14+
*/
15+
publicclassSolution {
16+
publicintsearch(int[]A,inttarget) {
17+
inti =0,j =A.length -1;
18+
while (i <=j) {
19+
intmid = (i +j) /2;
20+
if (A[mid] ==target)
21+
returnmid;
22+
if (A[i] <=A[mid]) {
23+
if (A[i] <=target &&target <A[mid])
24+
j =mid -1;
25+
else
26+
i =mid +1;
27+
}else {
28+
if (A[mid] <target &&target <=A[j])
29+
i =mid +1;
30+
else
31+
j =mid -1;
32+
}
33+
}
34+
return -1;
35+
}
36+
}

‎SearchinRotatedSortedArrayII.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Author: King, higuige@gmail.com
3+
Date: Nov 18, 2014
4+
Problem: Search in Rotated Sorted Array II
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/
7+
Notes:
8+
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
9+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
10+
What if duplicates are allowed?
11+
Would this affect the run-time complexity? How and why?
12+
Write a function to determine if a given target is in the array.
13+
14+
Solution: Sequence search. O(n)
15+
Since there are duplicates, it's hard to decide which branch to go if binary-search is deployed.
16+
*/
17+
publicclassSolution {
18+
publicbooleansearch_1(int[]A,inttarget) {
19+
for (inti =0;i <A.length; ++i) {
20+
if (A[i] ==target)returntrue;
21+
}
22+
returnfalse;
23+
}
24+
publicbooleansearch(int[]A,inttarget) {
25+
intleft =0,right =A.length -1;
26+
while (left <=right) {
27+
intmid = (left +right) /2;
28+
if (A[mid] ==target)returntrue;
29+
if (A[left] <A[mid]) {
30+
if (A[left] <=target &&target <A[mid])right =mid -1;
31+
elseleft =mid +1;
32+
}elseif (A[left] >A[mid]) {
33+
if (A[mid] <target &&target <=A[right])left =mid +1;
34+
elseright =mid -1;
35+
}else {
36+
if (A[left] ==A[right]) {
37+
++left; --right;
38+
}elseleft =mid +1;
39+
}
40+
}
41+
returnfalse;
42+
}
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp