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:
這次作業(yè)也還好丛版,因?yàn)橐呀?jīng)有了之前的思路。
可能太累了偏序,AC之后页畦,自己都忘記自己寫的什么思路了。研儒。豫缨。
應(yīng)該就是先確定,最小值會(huì)出現(xiàn)在哪一側(cè)端朵,然后再將目標(biāo)值與中間值比較好芭,其中會(huì)有個(gè)較為復(fù)雜的情況,即被搜索數(shù)可能會(huì)出現(xiàn)在mid兩側(cè)冲呢,此時(shí)需要再與 nums[end] 比較下栓撞,就可以做出進(jìn)一步的判斷了,然后再次遞歸。
以此類推瓤湘。
累死了。
**
總結(jié): 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;
}
}
這道題目我看以前我的解法是用遞歸來做的恩尾,這次直接用循環(huán)來做了弛说。
感覺差不多。
一個(gè)注意點(diǎn)翰意,也是我第一次提交沒通過的地方木人,
while (begin <= end) 記住,
在 find minimum element in rotated sorted array 中冀偶,是 <
而這里是醒第, <=
因?yàn)樵冢琭ind minimum element in rotated sorted array 中进鸠,當(dāng)begin == end時(shí)稠曼,就相當(dāng)于已經(jīng)找到了這個(gè)最小值。所以直接退出循環(huán)了客年。
而在search中霞幅,begin == end時(shí),還需要判斷下 nums[begin] 是否等于target量瓜。不能直接退出循環(huán)的司恳。
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
不用寫的這么復(fù)雜绍傲。
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
把它當(dāng)成一個(gè)正規(guī)的array去找扔傅,然后坐標(biāo)再轉(zhuǎn)換回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;
}
}
不要搞那么復(fù)雜猎塞。直接用這個(gè)方法。
如果有重復(fù)枫弟。 end--
Anyway, Good luck, Richardo! -- 09/26/2016