Question:

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.

Tips:

给定一个数组,该数组是由一个有序的数组经过旋转(即将前面一段数字接到整个数组之后)得到的。判断target是否存在于该数组之中。

数组中不存在重复数字,如果target存在,返回他的index不存在则返回-1.

思路:

查找某数字是否存在,可以使用二分查找,但是由于该数组不是正常的有序,不能使用正常的BS。

设置两个指针,low high分别指向数组的首尾,mid=(high+low)/2;旋转后的数组用mid分为俩半,必然有一半是有序的,先判断target是不是在有序的一半内,如果在直接进行二分搜索,如果不在那么就在另外一部分里面。

代码:

public int search(int[] nums, int target) {
if (nums == null || nums.length == 0)
return -1;
int len = nums.length ;
int low = 0;
int high = len - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target)
return mid;
if (nums[low] <= nums[mid]) {
if(target<nums[mid] && target>=nums[low]){
high=mid-1;
}else{
low=mid+1;
} }else{
if(target>nums[mid] && target<=nums[high]){
low=mid+1;
}else
high=mid-1;
} }
return -1;
}

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

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

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

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

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

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

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

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

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

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

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

  7. 【一天一道LeetCode】#33. Search in Rotated Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...

  8. 【LeetCode】033. Search in Rotated Sorted Array

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

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

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

随机推荐

  1. VS2015+OpenGL4.0开发编译时弹出错误:glaux.lib(tk.obj) : error LNK2019: 无法解析的外部符号 _sscanf,该符号在函数 _GetRegistrySysColors@8 中被引用

    一.问题描述: VS2015+OpenGL4.0开发编译时弹出如下所示的错误: 1>glaux.lib(tk.obj) : error LNK2019: 无法解析的外部符号 _sscanf,该符 ...

  2. day07--字符编码、文件处理

    今日内容: 字符编码 文件处理 字符编码: 把字符编码成二进制 各个国家拥有各自的字符编码,这样会导致交流产生问题.所以后面推出了内存使用unicode,硬盘使用UTF-8这个模式 unicode有两 ...

  3. js 根据相对路径url获得完整路径url

    自定义方法 GetPath(url) ///根据相对路径得到完整URL ///strUrl:URL相对地址 var GetPath = function (strUrl) { if (strUrl.t ...

  4. 微信小程序开发 [02] 页面注册和基本组件

    1.页面注册 既然我们希望跳转到新的页面,那自然要新建页面相关的文件才行.在开篇已经讲过,一个小程序页面由四个文件组成,假如我们的页面名为welcome,那么这四个文件则是: welcome.js w ...

  5. STM32 & FreeRTOS & KFIFO (巧夺天工)

    巧夺天工 的 KFIFO ,用STM32实现. 实现源文件如下: /********************************************************** * * 文件名 ...

  6. sql的一个查询,情景:a表中存在的数据,且在b表中不存在 (not in,not exists

    这里需要强调的是b表中关联字段的值是唯一的这种情况,并且b表尽量是列举类型的,意味着表比较小. ==================== 准备数据: 1. 建两个类似表,test1,test2,只有i ...

  7. 使用参数化查询防止SQL注入漏洞(转)

    SQL注入的原理 以往在Web应用程序访问数据库时一般是采取拼接字符串的形式,比如登录的时候就是根据用户名和密码去查询: string sql * FROM [User] WHERE UserName ...

  8. go语言之行--文件操作、命令行参数、序列化与反序列化详解

    一.简介 文件操作对于我们来说也是非常常用的,在python中使用open函数来对文件进行操作,而在go语言中我们使用os.File对文件进行操作. 二.终端读写 操作终端句柄常量 os.Stdin: ...

  9. 20155338 《网络攻防》Exp5 MSF基础应用

    20155338 <网络攻防>Exp5 MSF基础应用 基础问题回答 1. 用自己的话解释什么是exploit,payload,encode? exploit将真正要负责攻击的代码传送到靶 ...

  10. noip 提高组 2010

    T1:机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英 ...