-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathL0033_SearchInRotatedSortedArray.java
More file actions
78 lines (68 loc) · 3.14 KB
/
L0033_SearchInRotatedSortedArray.java
File metadata and controls
78 lines (68 loc) · 3.14 KB
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
/**
* https://leetcode.cn/problems/search-in-rotated-sorted-array/
*
* 整数数组 nums 按升序排列,数组中的值 互不相同 。
*
* 在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。
*
* 给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 target ,则返回它的下标,否则返回 -1 。
*
* 你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。
*/
public class L0033_SearchInRotatedSortedArray {
public int search(int[] nums, int target) {
// 特殊情况处理
if (nums == null || nums.length == 0) {
return -1;
}
int left = 0;
int right = nums.length - 1;
// 二分查找
while (left <= right) {
int mid = left + (right - left) / 2;
// 找到目标值
if (nums[mid] == target) {
return mid;
}
// 判断哪部分是有序的
if (nums[left] <= nums[mid]) { // 左半部分有序
// 判断目标值是否在左半部分
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1; // 在左半部分中继续查找
} else {
left = mid + 1; // 在右半部分中继续查找
}
} else { // 右半部分有序
// 判断目标值是否在右半部分
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1; // 在右半部分中继续查找
} else {
right = mid - 1; // 在左半部分中继续查找
}
}
}
return -1; // 未找到目标值
}
public static void main(String[] args) {
// 测试用例
L0033_SearchInRotatedSortedArray solution = new L0033_SearchInRotatedSortedArray();
// 测试用例 1
int[] nums1 = {4, 5, 6, 7, 0, 1, 2};
int target1 = 0;
System.out.println("测试用例 1:");
System.out.println("输入:nums = [4,5,6,7,0,1,2], target = 0");
System.out.println("输出:" + solution.search(nums1, target1)); // 预期输出:4
// 测试用例 2
int[] nums2 = {4, 5, 6, 7, 0, 1, 2};
int target2 = 3;
System.out.println("\n测试用例 2:");
System.out.println("输入:nums = [4,5,6,7,0,1,2], target = 3");
System.out.println("输出:" + solution.search(nums2, target2)); // 预期输出:-1
// 测试用例 3
int[] nums3 = {1};
int target3 = 0;
System.out.println("\n测试用例 3:");
System.out.println("输入:nums = [1], target = 0");
System.out.println("输出:" + solution.search(nums3, target3)); // 预期输出:-1
}
}