题目:

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.

题解:

Solution 1

class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
if(n < )
return -;
int begin = , end = n;
while(begin < end){
int mid = begin + (end - begin) / ;
if(nums[mid] == target)
return mid;
if(nums[begin] <= nums[mid]){
if(nums[begin] <= target && target < nums[mid])
end = mid;
else
begin = mid + ;
} else {
if(nums[mid] < target && target <= nums[end - ])
begin = mid + ;
else
end = mid;
}
}
return -;
}
};

Solution 2

class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
if(n < )
return -;
int begin = , end = n - ;
while(begin < end){
int mid = begin + (end - begin) / ;
if(nums[mid] > nums[end])
begin = mid + ;
else
end = mid;
}
int pivot = begin;
begin = , end = n - ;
while(begin <= end){
int mid = begin + (end - begin) / ;
int realmid = (mid + pivot) % n;
if(nums[realmid] == target)
return realmid;
else if(nums[realmid] < target)
begin = mid + ;
else
end = mid - ;
}
return -;
}
};

【LeetCode】033. Search in Rotated Sorted Array的更多相关文章

  1. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

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

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

  3. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  4. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  5. 【LeetCode】081. Search in Rotated Sorted Array II

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

  6. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

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

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

  9. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

随机推荐

  1. 使用3DES+Base64来加密传输iOS应用数据

    本文转载至 http://www.erblah.com/post/objective-c/shi-yong-3des-base64lai-jia-mi-chuan-shu-iosying-yong-s ...

  2. python 基础 9.11 更改数据

    #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/24 4:45 #@Auther :liuzhenchuan #@File   :更改 ...

  3. 九度OJ 1184:二叉树遍历 (二叉树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3515 解决:1400 题目描述: 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的 ...

  4. 【HTTP】HTPP学习笔记

    1.了解web及网络基础 HTTP的诞生 TCP/IP协议族 应用层 FTP文件传输协议 HTTP超文本传输协议 DNS域名系统:IP地址<--->域名 传输层 TCP传输控制协议 三次握 ...

  5. linux 5-sort,uniq,tar,split

    十二.   行的排序命令sort:   1.  sort命令行选项: 选项 描述 -t 字段之间的分隔符 -f 基于字符排序时忽略大小写 -k 定义排序的域字段,或者是基于域字段的部分数据进行排序 - ...

  6. Python基础(4)_字典、集合、bool值

    三.字典 定义:{key1:value1,key2:value2},key-value结构,key必须是不可变类型,或者可hash 基本形式:key:value特性: 1.可存放多个值 2.可修改指定 ...

  7. rails 注释

    view -# js.erb <%# xxxx %> 单行注释

  8. swift 全局常量 && 全局变量的写法

    在OC里面 如果 想设置一个全局常量 很简单 使用简单宏定义  就搞定了 例如: #define    WEBAPIBASEURL  @"http://www.baidu.com/" ...

  9. c的详细学习(10)结构体与共用体的学习(二)

    在c语言中,结构体数据类型与共用体数据类型都属于构造类型.共用体与结构体数据类型在定义上十分相似,但它们在存储空间的占用分配上有本质的区别.结构体变量是各种类型数据的集合,各成员占据不同的存储空间,而 ...

  10. 【leetcode刷题笔记】Search in Rotated Sorted Array II

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