GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/SearchInRotatedSortedArray.java at master · LeBW/leetcode

Expand file tree

/

SearchInRotatedSortedArray.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
98 lines (91 loc) · 2.97 KB
/

SearchInRotatedSortedArray.java

Copy path

File metadata and controls

98 lines (91 loc) · 2.97 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* 33. Search in Rotated Sorted Array
* 二分查找法。
* 在找到 mid 之后,至少有一侧:[left, mid] 或者 [mid, right] 是有序的。
* 所以我们可以先判断在不在有序的这一侧(if),从而决定接下来在哪一侧寻找。
* @author LBW
*/
public class SearchInRotatedSortedArray {
public int search(int[] nums, int target) {
int len = nums.length;
if (len == 0)
return -1;
int result = findIn(nums, 0, len-1, target);
return result;
}
private int findIn(int[] nums, int l, int r, int target) {
if (l > r)
return -1;
else if (l == r)
return nums[l] == target ? l : -1;
if (nums[l] == target)
return l;
if (nums[r] == target)
return r;
int mid = (l + r) / 2;
if (nums[mid] == target)
return mid;
if (nums[mid] > nums[l]) { // the rotate part is in right part.
if (target > nums[l] && target < nums[mid]) {
return binarySearch(nums, l+1, mid-1, target);
}
else {
return findIn(nums, mid+1, r-1, target);
}
}
else {
if (target > nums[mid] && target < nums[r]) {
return binarySearch(nums, mid+1, r-1, target);
}
else {
return findIn(nums, l+1, mid-1, target);
}
}
}
private int binarySearch(int[] nums, int l, int r, int target) {
if (l > r)
return -1;
else if (l == r)
return nums[l] == target ? l : -1;
if (nums[l] == target)
return l;
else if (nums[r] == target)
return r;
int mid = (l + r) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] > target)
return binarySearch(nums, l+1, mid-1, target);
else
return binarySearch(nums, mid+1, r-1, target);
}
// 方法二:标准的二分查找
public int searchTwo(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1;
while (left < right) {
int mid = left + (right - left) / 2; // 这里的 mid 是向下取整
if (nums[mid] == target) {
return mid;
}
if (nums[mid] >= nums[left]) { // 说明[left, mid]是有序的
if (target < nums[mid] && target >= nums[left]) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
else { // 说明 [mid, right] 是有序的
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
}
if (nums[left] == target)
return left;
return -1;
}}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.