My code:
public class Solution {
public boolean search(int[] nums, int target) {
if (nums == null || nums.length == 0)
return false;
return search(0, nums.length - 1, nums, target);
}
private boolean search(int begin, int end, int[] nums, int target) {
if (begin > end)
return false;
else {
int mid = (begin + end) / 2;
if (nums[mid] > nums[end]) { // left is sorted
if (nums[mid] < target) {
int i = mid + 1;
while (i <= end && nums[i] == nums[i - 1])
i++;
if (i >= nums.length)
return false;
else
return search(i, end, nums, target);
}
else if (nums[mid] > target) {
if (nums[begin] > target) { // in the right
int i = mid + 1;
while (i <= end && nums[i] == nums[i - 1])
i++;
if (i >= nums.length)
return false;
else
return search(i, end, nums, target);
}
else if (nums[begin] == target)
return true;
else { // in the left
int i = mid - 1;
while (i >= begin && nums[i] == nums[i + 1])
i--;
if (i < 0)
return false;
else
return search(begin + 1, i, nums, target);
}
}
else
return true;
}
else if (nums[mid] < nums[end]) { // right is sorted
if (nums[mid] > target) {
int i = mid - 1;
while (i >= begin && nums[i] == nums[i + 1])
i--;
if (i < 0)
return false;
else
return search(begin, i, nums, target);
}
else if (nums[mid] < target) {
if (target < nums[end]) {
int i = mid + 1;
while (i <= end && nums[i] == nums[i - 1])
i++;
if (i >= nums.length)
return false;
else
return search(i, end, nums, target);
}
else if (target > nums[end]) {
int i = mid - 1;
while (i >= begin && nums[i] == nums[i + 1])
i--;
if (i < 0)
return false;
else
return search(begin, i, nums, target);
}
else
return true;
}
else
return true;
}
else
if (nums[mid] == target)
return true;
else
return search(begin, end - 1, nums, target);
}
}
}
My test result:
這道題目還是和前面的差不多光羞。然后就是要用while循環(huán)把重復(fù)的數(shù)字跳到。
然后就是同樣的注意點四瘫,當nums[mid] = nums[end] 時捶索,可能出現(xiàn)兩種情況八孝,右側(cè)已經(jīng)排序好或者左側(cè)已經(jīng)排序好董朝。所以不能確定。
于是干跛,就直接將end - 1子姜,重新進行搜索。
所以楼入,如果數(shù)組數(shù)字都是一樣的哥捕,需要進行 O(n)復(fù)雜度,而不再是之前的binary search 的 O(log n)
**
總結(jié): Array嘉熊, binary search
**
Anyway, Good luck, Richardo!
My code:
public class Solution {
public boolean search(int[] nums, int target) {
if (nums == null || nums.length == 0)
return false;
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 true;
}
else if (target <= nums[end]) {
begin = middle + 1;
}
else {
end = middle - 1;
}
}
else if (nums[middle] > nums[end]) { // left part is sorted
if (target > nums[middle]) {
begin = middle + 1;
}
else if (target == nums[middle]) {
return true;
}
else if (target >= nums[0]) {
end = middle - 1;
}
else {
begin = middle + 1;
}
}
else {
if (target == nums[middle])
return true;
else
end = end - 1;
}
}
return false;
}
}
差不多的題目遥赚。就把之前的情況細分。
nums[middle] < nums[end]
nums[middle] > nums[end]
nums[middle] == nums[end] -> end - 1
差不多阐肤。然后因為出現(xiàn)了重復(fù)元素所以可以滑動凫佛,用while循環(huán)加速讲坎。
但是我懶得寫了。第一次的版本是寫了的愧薛。
Anyway, Good luck, Richardo!
提供一種新的思路就是直接traverse晨炕, 復(fù)雜度是 O(n)
上面的做法, time complexity: best: O(log n), worst: O(n)
Anyway, Good luck, Richardo! -- 08/12/2016
My code:
public class Solution {
public boolean search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return false;
}
int begin = 0;
int end = nums.length - 1;
while (begin <= end) {
int mid = begin + (end - begin) / 2;
if (target == nums[mid]) {
return true;
}
else if (nums[begin] < nums[mid]) { // left is sorted
if (nums[begin] <= target && target < nums[mid]) {
end = mid - 1;
}
else {
begin = mid + 1;
}
}
else if (nums[begin] > nums[mid]) { // right is sorted
if (nums[mid] < target && target <= nums[end]) {
begin = mid + 1;
}
else {
end = mid - 1;
}
}
else {
begin++;
}
}
return false;
}
}
much simpler code
首先判斷毫炉,左右那個部分是 sorted 瓮栗,然后再進行下一步。
Anyway, Good luck, Richardo! -- 09/21/2016