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

Appearance settings

Latest commit

 

History

History
History
31 lines (30 loc) · 892 Bytes

File metadata and controls

31 lines (30 loc) · 892 Bytes
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
package leetcode.binarySearch;
public class leetcode33 {
public int search(int[] nums, int target) {
int left=0;
int right=nums.length-1;
int mid=(left+right)/2; //中点
while(left<=right){
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; //只能从左边找
}
}
mid=(left+right)/2;
}
return -1; //没找到
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.