Skip to content

33 (java)、题目编号统一修改为四位数 #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions solution/033.Search in Rotated Sorted Array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int search(int[] A, int target) {
if (A == null || A.length == 0) return -1;
int low = 0,high = A.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (target < A[mid]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mid,此写法有溢出的可能噢,换成以下写法可能会好一些哈~

int mid = low + ((hight - low) >> 1);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,没注意。

if (A[mid] >= A[high] && target < A[low]) low = mid + 1;
else high = mid - 1;
} else if (target > A[mid]) {
if (A[low] >= A[mid] && target > A[high]) high = mid - 1;
else low = mid + 1;
} else return mid;
}
return -1;
}
}