-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33.搜索旋转排序数组.java
44 lines (41 loc) · 1 KB
/
33.搜索旋转排序数组.java
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
/*
* @lc app=leetcode.cn id=33 lang=java
*
* [33] 搜索旋转排序数组
*/
// @lc code=start
class Solution {
public int search(int[] nums, int target) {
/* int res = -1;
for(int i = 0 ;i < nums.length; i++){
if(nums[i] == target){
res = i;
return res;
}
}
return res;
*/
if(nums[0] > target && nums[nums.length -1] < target){
return -1;
}
else if(nums[0] > target && nums[nums.length - 1] >= target){
for(int i = nums.length - 1; i >= 0; i--){
if(nums[i] == target){
return i;
}
}
}
else if(nums[0] == target){
return 0;
}
else if(nums[0] < target){
for(int i = 0;i < nums.length; i++){
if(nums[i] == target){
return i;
}
}
}
return -1;
}
}
// @lc code=end