33.[LeetCode] Search in Rotated Sorted Array
Suppose an array sorted in ascending order 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.
Your algorithm's runtime complexity must be in the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -133. Search in Rotated Sorted Array
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = , r = nums.size()-;
while (l <= r) {
int mid = (l+r) / ;
if (target == nums[mid])
return mid;
// there exists rotation; the middle element is in the left part of the array
if (nums[mid] > nums[r]) {
if (target < nums[mid] && target >= nums[l])
r = mid - ;
else
l = mid + ;
}
// there exists rotation; the middle element is in the right part of the array
else {
if (target > nums[mid] && target <= nums[r])
l = mid + ;
else
r = mid - ;
}
}
return -;
}
};
33.[LeetCode] Search in Rotated Sorted Array的更多相关文章
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
- 【LeetCode 33】Search in Rotated Sorted Array
Search in Rotated Sorted Array 分段有序的数组,二分查找返回下标,没有返回-1 数组有序之后经过 rotated, 比如:6 1 2 3 4 5 or 5 6 7 8 ...
- [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- LeetCode: Search in Rotated Sorted Array 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
随机推荐
- SQLIO 磁盘測试工具參考
SQLIO 下载地址:id=20163">SQLIO Disk Subsystem Benchmark Tool 默认文件夹:C:\Program Files\SQLIO 以命令行执行 ...
- Linux文本处理
作为一名 Linux 研发人员,几乎每天都要面对文本处理场景. 因此 掌握文本处理套路 并 熟练运用文本处理命令 ,对于 提升工作效率 意义重大. 本文以一个实战例子抛砖引玉,介绍如何运用 grep ...
- java的多线程和并发库
一.多线程基础知识 1.传统使用类Thread和接口Runnable实现 1)在Thread子类覆盖的run方法中编写运行代码 2)在传递给Thread对象的Runnable对象的run方法中编写代码 ...
- OpenID Connect Core 1.0(八)从第三方发起登录
在某些情况下,登录流程由一个OpenID提供者或其他方发起,而不是依赖方(RP).在这种情况下,发起者重定向到RP在发起登录终结点,RP的请求验证请求发送到指定的OP.这个发起登录终结点可以在RP深度 ...
- 使用属性Props完成一张卡片
一:我们先安装bootstrap,为了使我们的样式好看些 cnpm install bootstrap --save 二:我们在index.js中引入bootstap Import ‘bootst ...
- Ionic3项目实践记录
Ionic3首次项目实践记录 标签(空格分隔): Angular Ionic Ionic3踩坑 1. 路由懒加载(lazy load) 如果设置了懒加载,就必须全部懒加载(包括TabsPage),否则 ...
- C++练习 | 创建并倒序输出不带头结点的链表
#include <iostream> #include <cstdio> #include <stdlib.h> #include <stack> u ...
- 偏前端-vue.js学习之路初级(一)概念
首先--不推荐新手直接使用 vue-cli,尤其是在你还不熟悉基于 Node.js 的构建工具时. 新建一个html,引入一下js: <!-- 开发环境版本,包含了有帮助的命令行警告 -- ...
- Field Security Profile Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; /// <summary> /// 安全字 ...
- 远程查看java虚拟机内存使用情况jconsole
jconsole 查看虚拟机使用情况 1.在远程机的tomcat的catalina.sh中加入配置: JAVA_OPTS="$JAVA_OPTS -Djava.rmi.server.host ...