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.

解题:

二分搜索的应用,可以有两种思路:

(1)将数列从mid处划分,按照前半段序列顺序正常,或者不正常,进行条件判断,达到log(n)的二分效果;

(2)将数列从mid处划分,比较mid处元素和target,按照mid元素大,或者mid元素小,进行条件判断,达到log(n)的二分效果;

代码实现1:

代码简介,灵活的运用的二分的思想,将正常/非正常作为二分标准,而不是mid值大还是小。

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

代码实现2:

思路更常规,虽然代码不如1简介,但是逻辑更好理解。

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

【Leetcode】【Hard】Search in Rotated Sorted Array的更多相关文章

  1. LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...

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

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

  3. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

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

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

  5. 【leetcode】Search in Rotated Sorted Array II

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

  6. 【leetcode】Search in Rotated Sorted Array

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

  7. 【leetcode】Search in Rotated Sorted Array II(middle)☆

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

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

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

  9. 【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 ...

  10. 【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 ...

随机推荐

  1. Java_break与continue区别

    使用场合不同 break:可以在switch case中使用,也可以在循环中使用   continue:只能在循环中使用,除了switch case 作用不同 break:表示中断,当在switch ...

  2. 实验Complex

    #include<iostream> #include<cmath> using namespace std; class Complex { public: Complex ...

  3. LinuxShell脚本编程7-for和while

    1.for的使用 #! /bin/bash ` do echo $a done 表示:a初始值为1,然后a=a+2的操作,一直到a<=10为止 for((i=1;i<=10;i=i+2)) ...

  4. CSS ::Selection的使用方法

    大家都知道浏览器对选中的文本默认样式都是统一的,Windows下是一个深蓝色的背景,白字的前景,而在Mac下是一个淡蓝色背景,白色字体,就如上图所展示的一样,自从有了这个“::selection”选择 ...

  5. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

  6. 九度oj 1032 ZOJ 2009年浙江大学计算机及软件工程研究生机试真题

    题目1032:ZOJ 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4102 解决:2277 题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当 ...

  7. 通过IntelliJ IDEA忽略掉不需要提交到github的文件

    现如今,开源项目越来越多,存储容器主要有github,国内的码云.开源贡献也是衡量一个开发者是否具有足够的包容能力.技术能力的重要标准. 有些开发者没注意到这些,好心提交开源项目,配置文件也提交上去, ...

  8. Redis学习笔记--常用命令

    以下为本人学习Redis的备忘录,记录了大部分常用命令 1.客户端连接redis服务端: ===启动Redis服务端 redis-server /yourpath/redis.conf ===启动Re ...

  9. Restful的理解,Restful 优缺点

    写一下我对restful的理解,最近换工作面试的时候有问到我restful api的东西,工作中以前很多项目也是webapi + js前台控件的形式构建系统.实际上感觉restful太“理想化”,用起 ...

  10. springmvc4集成swagger2

    首先在原有的springmvc工程的pom文件中增加swagger <dependency> <groupId>io.springfox</groupId> < ...