My code:
public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0)
return 0;
return search(0, nums.length - 1, target, nums);
}
private int search(int begin, int end, int target, int[] nums) {
if (begin > end )
return -1;
int mid = (begin + end) / 2;
if (nums[mid] < nums[end]) {// right part is sorted so minimun exists in the left
if (target < nums[mid])
return search(begin, mid - 1, target, nums);
else if (target > nums[mid])
if (target > nums[end])
return search(begin, mid - 1, target, nums);
else
return search(mid + 1, end, target, nums);
else
return mid;
}
else {// left part is sorted so minimun exists in the right
if (target > nums[mid])
return search(mid + 1, end, target, nums);
else if (target < nums[mid])
if (target <= nums[end])
return search(mid + 1, end, target, nums);
else
return search(begin, mid - 1, target, nums);
else
return mid;
}
}
}
My test result:
这次作业也还好,因为已经有了之前的思路。
可能太累了,AC之后,自己都忘记自己写的什么思路了。。。
应该就是先确定,最小值会出现在哪一侧,然后再将目标值与中间值比较,其中会有个较为复杂的情况,即被搜索数可能会出现在mid两侧,此时需要再与 nums[end] 比较下,就可以做出进一步的判断了,然后再次递归。
以此类推。
累死了。
**
总结: Array, BinarySearch
**
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0)
return -1;
int begin = 0;
int end = nums.length - 1;
while (begin <= end) {
int middle = (begin + end) / 2;
if (nums[middle] < nums[end]) { // right part is sorted
if (target < nums[middle]) {
end = middle - 1;
}
else if (target == nums[middle]) {
return middle;
}
else if (target <= nums[end]) {
begin = middle + 1;
}
else {
end = middle - 1;
}
}
else { // left part is sorted
if (target > nums[middle]) {
begin = middle + 1;
}
else if (target == nums[middle]) {
return middle;
}
else if (target >= nums[0]) {
end = middle - 1;
}
else {
begin = middle + 1;
}
}
}
return -1;
}
}
这道题目我看以前我的解法是用递归来做的,这次直接用循环来做了。
感觉差不多。
一个注意点,也是我第一次提交没通过的地方,
while (begin <= end) 记住,
在 find minimum element in rotated sorted array 中,是 <
而这里是, <=
因为在,find minimum element in rotated sorted array 中,当begin == end时,就相当于已经找到了这个最小值。所以直接退出循环了。
而在search中,begin == end时,还需要判断下 nums[begin] 是否等于target。不能直接退出循环的。
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int begin = 0;
int end = nums.length - 1;
while (begin <= end) {
int middle = begin + (end - begin) / 2;
if (target < nums[middle]) {
if (nums[middle] > nums[end]) {
if (target < nums[end]) {
begin = middle + 1;
}
else if (target > nums[end]) {
end = middle - 1;
}
else {
return end;
}
}
else {
end = middle - 1;
}
}
else if (target > nums[middle]) {
if (nums[middle] > nums[end]) {
begin = middle + 1;
}
else {
if (target > nums[end]) {
end = middle - 1;
}
else if (target < nums[end]) {
begin = middle + 1;
}
else {
return end;
}
}
}
else {
return middle;
}
}
return -1;
}
}
差不多的思路。
Anyway, Good luck, Richardo! -- 08/12/2016
不用写的这么复杂。
My code:
public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int lo = 0;
int hi = nums.length - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] < nums[hi]) {
hi = mid;
}
else {
lo = mid + 1;
}
}
int rotate = lo;
lo = 0;
hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int realMid = (mid + rotate) % nums.length;
if (nums[realMid] == target) {
return realMid;
}
else if (nums[realMid] > target) {
hi = mid - 1;
}
else {
lo = mid + 1;
}
}
return -1;
}
}
reference:
https://discuss.leetcode.com/topic/3538/concise-o-log-n-binary-search-solution/2
把它当成一个正规的array去找,然后坐标再转换回rotate的array,去取出我们需要的中间值。
Anyway, Good luck, Richardo! -- 09/12/2016
My code:
public class Solution {
public int search(int[] nums, int target) {
int begin = 0;
int end = nums.length - 1;
while (begin <= end) {
int mid = begin + (end - begin) / 2;
if (nums[mid] == target) {
return mid;
}
else if (nums[mid] < nums[end]) { // right is sorted
if (nums[mid] < target && target <= nums[end]) {
begin = mid + 1;
}
else {
end = mid - 1;
}
}
else { // left is sorted
if (nums[begin] <= target && target < nums[mid]) {
end = mid - 1;
}
else {
begin = mid + 1;
}
}
}
return -1;
}
}
不要搞那么复杂。直接用这个方法。
如果有重复。 end--
Anyway, Good luck, Richardo! -- 09/26/2016