33. 81. Search in Rotated Sorted Array *HARD*
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*的更多相关文章
- 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 ...
- 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 ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- 81 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- [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. 思路 ...
- 【Leetcode】81. Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...
- [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. ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- MySQL Crash Course #09# Chapter 17. Combining Queries: UNION
INDEX UNION Rules WHERE VS. UNION UNION VS. UNION ALL Sorting Combined Query Results UNION Rules As ...
- mysql 触发器 trigger用法 two (稍微复杂的)
触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...
- ELK学习笔记之Logstash详解
0x00 Logstash概述 官方介绍:Logstash is an open source data collection engine with real-time pipelining cap ...
- 怎么说, 开发会很乐意去主动修改bug?
怎么说, 开发会很乐意去主动修改bug? 一图顶上千言万语,如下:
- ThinkPHP5跨控制器调用
1.在application\index\controller\文件夹里新建User.php <?php namespace app\index\controller; class User{ ...
- TCP客户端【TcpClient】
一.阻塞模式 1.命名空间 System.Net.Sockets 2.对象声明 TcpClient dpu1TcpClient = null;//dpu1tcp客户端,TcpClient模式 Netw ...
- Delphi XE5 for Android (五)
Android程序开发必然用到按钮,在XE5下,按钮的一个比较重要的属性就是StyleLookup,预置了一系列常用的图标,如下图: 另外2个常用属性就是: GroupName和IsPressed:一 ...
- Python3基础 str + 字符串变量拼接
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- C#创建继承的窗体
http://blog.csdn.net/chenyujing1234/article/details/7555369 关键技术 基窗体,实质上相当于面向对象编程中提到的基类,而继承窗体则是子类或派生 ...
- 总结java中的super和this关键字
知识点: 在java类中使用super引用父类的成分,用this引用当前对象 this可以修饰属性.构造器.方法 super可以修饰属性.构造器.方法 关于子类实例化过程中的内存分配,在下一篇博客中说 ...