33. Search in Rotated Sorted Array
2016-06-15 19:58
405 查看
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
1.我的解答
就是找最大的那个值,然后一分为二,然后target就从这两半中找
class Solution {
public:
int search(int target, vector<int>& num, int begin, int end){
int b = begin, e = end, m = 0;
if((begin > end) || (begin < 0))
return -1;
while(b < e){
m = b + (e - b)/2;
if(target > num[m])
b = m + 1;
else if(target < num[m])
e = m - 1;
else
return m;
}
if(num[b] == target)
return b;
else
return -1;
}
int search(vector<int>& nums, int target) {
int n = nums.size();
int b = 0, e = n - 1, m = 0;
while(e - b > 1){
m = b + (e - b)/2;
if((nums[m] > nums[b]) && (nums[m]) > nums[e])
b = m;
else if((nums[m] < nums[b]) && (nums[m] < nums[e]))
e = m;
else
break;
}
int big = 0;
if(nums[b] > nums[e])
big = b;
else
big = e;
if(target > nums[0])
return search(target,nums,0,big);
else if(target < nums[0])
return search(target,nums,big+1,n-1);
else
return 0;
}
};
2.当然。。。。大神的思路永远比我的好,这里贴上他的解题思路和代码,供以后自己看
永远尊重别人的成果!
Remember the array is sorted, except it might drop at one point.
If nums[0] <= nums[i], then nums[0..i] is sorted (in case of "==" it's just one element, and in case of "<"
there must be a drop elsewhere). So we should keep searching in nums[0..i] if the target lies in this sorted range, i.e., if
If nums[i] < nums[0], then nums[0..i] contains a drop, and thus nums[i+1..end] is sorted and lies strictly between nums[i] and nums[0]. So we should keep searching in nums[0..i]
if the target doesn't lie strictly between them, i.e., if
Those three cases look cyclic:
So I have the three checks
two of them are true. They can't all be true or all be false (check it), so I just need to distinguish between "two true" and "one true". Parity is enough for that, so instead of adding them I xor them, which is a bit shorter and particularly helpful in Java
and Ruby, because those don't let me add booleans but do let me xor them.
代码:
int search(vector<int>& nums, int target) {
int lo = 0, hi = int(nums.size()) - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if ((nums[0] > target) ^ (nums[0] > nums[mid]) ^ (target > nums[mid]))
lo = mid + 1;
else
hi = mid;
}
return lo == hi && nums[lo] == target ? lo : -1;
}
(i.e.,
0 1 2 4 5 6 7might become
4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
1.我的解答
就是找最大的那个值,然后一分为二,然后target就从这两半中找
class Solution {
public:
int search(int target, vector<int>& num, int begin, int end){
int b = begin, e = end, m = 0;
if((begin > end) || (begin < 0))
return -1;
while(b < e){
m = b + (e - b)/2;
if(target > num[m])
b = m + 1;
else if(target < num[m])
e = m - 1;
else
return m;
}
if(num[b] == target)
return b;
else
return -1;
}
int search(vector<int>& nums, int target) {
int n = nums.size();
int b = 0, e = n - 1, m = 0;
while(e - b > 1){
m = b + (e - b)/2;
if((nums[m] > nums[b]) && (nums[m]) > nums[e])
b = m;
else if((nums[m] < nums[b]) && (nums[m] < nums[e]))
e = m;
else
break;
}
int big = 0;
if(nums[b] > nums[e])
big = b;
else
big = e;
if(target > nums[0])
return search(target,nums,0,big);
else if(target < nums[0])
return search(target,nums,big+1,n-1);
else
return 0;
}
};
2.当然。。。。大神的思路永远比我的好,这里贴上他的解题思路和代码,供以后自己看
永远尊重别人的成果!
Remember the array is sorted, except it might drop at one point.
If nums[0] <= nums[i], then nums[0..i] is sorted (in case of "==" it's just one element, and in case of "<"
there must be a drop elsewhere). So we should keep searching in nums[0..i] if the target lies in this sorted range, i.e., if
nums[0] <= target <= nums[i].
If nums[i] < nums[0], then nums[0..i] contains a drop, and thus nums[i+1..end] is sorted and lies strictly between nums[i] and nums[0]. So we should keep searching in nums[0..i]
if the target doesn't lie strictly between them, i.e., if
target <= nums[i] < nums[0]or
nums[i] < nums[0] <= target
Those three cases look cyclic:
nums[0] <= target <= nums[i] target <= nums[i] < nums[0] nums[i] < nums[0] <= target
So I have the three checks
(nums[0] <= target),
(target <= nums[i])and
(nums[i] < nums[0]), and I want to know whether exactly
two of them are true. They can't all be true or all be false (check it), so I just need to distinguish between "two true" and "one true". Parity is enough for that, so instead of adding them I xor them, which is a bit shorter and particularly helpful in Java
and Ruby, because those don't let me add booleans but do let me xor them.
代码:
int search(vector<int>& nums, int target) {
int lo = 0, hi = int(nums.size()) - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if ((nums[0] > target) ^ (nums[0] > nums[mid]) ^ (target > nums[mid]))
lo = mid + 1;
else
hi = mid;
}
return lo == hi && nums[lo] == target ? lo : -1;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性