Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might 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.

利用二分查找的思想。

int binary_search(vector<int>& nums, int left, int right, int target)
{
if(left > right)
return -;
while(left <= right)
{
int mid = (left+right)>>;
if(nums[mid] == target)
return mid;
else if(nums[mid] < target)
left = mid+;
else
right = mid-;
}
return -;
}
int search(vector<int>& nums, int target) {
int n = nums.size(), left = , right = n-, mid;
if(left == right && nums[] == target)
return ;
while(left <= right)
{
mid = (left+right)>>;
if(target == nums[mid])
return mid;
if(nums[left] <= nums[mid])
{
if(target >= nums[left] && target <= nums[mid])
return binary_search(nums, left, mid, target);
left = mid+;
}
else
{
if(target >= nums[mid] && target <= nums[right])
return binary_search(nums, mid, right, target);
right = mid-;
}
}
return -;
}

What if duplicates are allowed?

class Solution {
public:
bool search(vector<int>& nums, int target) {
int n = nums.size(), left = , right = n-, mid;
bool found = false;
while(left <= right)
{
mid = (left+right) >> ;
if(nums[mid] == target)
return true;
if(nums[left] < nums[mid])
{
if(target >= nums[left] && target < nums[mid])
right = mid - ;
else
left = mid + ;
}
else if(nums[left] > nums[mid])
{
if(target > nums[mid] && target <= nums[right])
left = mid + ;
else
right = mid - ;
}
else
left++;
}
return false;
}
};

33. 81. Search in Rotated Sorted Array *HARD*的更多相关文章

  1. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  2. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. 81 Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  6. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  7. 【Leetcode】81. Search in Rotated Sorted Array II

    Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...

  8. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  9. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

随机推荐

  1. python的时间处理-time模块

    time模块 时间的表示方法有三种: 时间戳:表示的是从1970年1月1日0点至今的秒数 格式化字符串表示:这种表示更习惯我们通常的读法,如2018-04-24 00:00:00 格式化元祖表示:是一 ...

  2. Quartz框架调用——运行报错:ThreadPool class not specified

    Quartz框架调用——运行报错:ThreadPool class not specified 问题是在于Quartz框架在加载的时候找不到quartz.properties配置文件: 解决方案一: ...

  3. Mysql的基本语句

    Mysql的基本语句 1.查询当前数据库所有表名: -- 方案一: show tables; --方案二:jeesite为数据库 select table_name from information_ ...

  4. java replaceAll之$替换

    最近,在开发一个伪代码翻译工具的过程中,遇到个问题,我们在伪代码中使用spring EL的规则,将非变量限定在${"1"}中,解析后根据上下文的类型转换为相应的java类型.在规则 ...

  5. 一款简单实用的jQuery图片画廊插件

    图片画廊 今天分享一个自己实现的jQuery 图片画廊插件. 看一下效果图: 点击图片时: 在线演示地址:http://www.jr93.top/photoGallery/photoGallery.h ...

  6. Centos7.2 修改网卡名称

    查看ip [root@localhost network-scripts]# ip addr : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue ...

  7. JavaScript:正则表达式 前瞻 找位置

    js中全部都是顺序环视 顺序环视匹配过程 对于顺序肯定环视(?=Expression)来说,当子表达式Expression匹配成功时,(?=Expression)匹配成功,并报告(?=Expressi ...

  8. Open Source Log Management

    https://www.elastic.co/solutions/logging The Elastic Stack (sometimes known as the ELK Stack) is the ...

  9. C# 测算代码运行时间 Stopwatch

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); stop ...

  10. BZOJ 1005: [HNOI2008]明明的烦恼(prufer数列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1005 题意: Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标 ...