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

思路:此题算法上不难。第一步计算旋转的步长。然后依据步长分情况得到升序的新的数组。然后二分查找target的索引。找到后再分情况返回元素的位置。

详细代码例如以下:

public class Solution {
public int search(int[] nums, int target) {
if(nums.length == 0){
return -1;
} if(nums.length == 1){
if(nums[0] == target)
return 0;
return -1;
} int rotate = 0;//旋转的步长
for(int i = nums.length - 1; i > 0; i--){
if(nums[i] < nums[i-1]){
rotate = i;
break;
}
}
int[] a = new int[nums.length];
//将数组按升序填充到新数组
for(int i = rotate; i < nums.length; i++){
a[i - rotate] = nums[i];//后面未旋转部分
}
for(int i = 0; i < rotate; i++){
a[i+nums.length-rotate] = nums[i];//前面旋转部分
} int index = -1;
//二分查找
int low = 0;
int hight = nums.length - 1;
while(low <= hight){
int mid = (low + hight)/2;
if(a[mid] > target){
hight = mid - 1;
}else if(a[mid] == target){
index = mid;
break;
}else{
low = mid + 1;
}
}
if(index == -1)
return -1;
if(index + rotate > nums.length - 1)
return index + rotate - nums.length;
return index + rotate;
}
}

兴许:这一题实在解的有些多余了。最简单的就是一次遍历。找到返回index,找不到返回-1.

代码例如以下:

public class Solution {
public int search(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
if(nums[i] == target){
return i;
}
}
return -1;
}
}

leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法的更多相关文章

  1. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  2. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  3. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  4. [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. 思路 ...

  5. LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  6. LeetCode 33.Search in Rotated Sorted Array(M)

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

  7. leetcode 33. 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 ...

  8. Java [leetcode 33]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 ...

  9. [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. Ubuntu下如何解压缩zip,tar,tar.gz,tar.bz2文件

    转自:http://wangli-5665.diandian.com/post/2011-08-18/4039228 这么多年来,数据压缩对我们来说是非常有用的.无论是在邮件中发送的图片用的zip文件 ...

  2. mysql show variables

    1. back_log 指定MySQL可能的连接数量.当MySQL主线程在很短的时间内得到非常多的连接请求,该参数就起作用,之后主线程花些时间(尽管很短)检查连接并且启动一个新线程. back_log ...

  3. 阅读ANSI C,寻找乐趣和裨益——const char **与char **为何不兼容

    #include<stdio.h> void foo1(const char**p) { } void foo2(const char*p) { } int main(int argc,c ...

  4. c++之——template模板函数

    为了实现与数据类型无关的编程,模板应运而生: #include<iostream> #include<string.h> using namespace std; templa ...

  5. GIS+=地理信息+行业+大数据——基于云环境流处理平台下的实时交通创新型app

    应用程序已经是近代的一个最重要的IT创新.应用程序是连接用户和数据之间的桥梁,提供即时訪问信息是最方便且呈现的方式也是easy理解的和令人惬意的. 然而,app开发人员.尤其是后端平台能力,一直在努力 ...

  6. maven 打包如何将依赖打进来

    阿斯蒂芬 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupI ...

  7. vshare

    vshare 基于百度分享开发的支持VUE2.X的分享插件,为您带来更多的流量!提供多种风格按钮,代码加载更快,引入社会化流量,提升网页抓取速度等优点.github地址:https://github. ...

  8. Zookeeper使用场景

    分布式系统的运行是很复杂的,因为涉及到了网络通信还有节点失效等不可控的情况.下面介绍在最传统的master-workers模型,主要可以会遇到什么问题,传统方法是怎么解决以及怎么用zookeeper解 ...

  9. iOS边练边学--UITableViewCell的常见属性设置

    // 取消选中的样式(常用) 让当前 cell 按下无反应 cell.selectionStyle = UITableViewCellSelectionStyleNone; // 设置选中的背景色,U ...

  10. 传说中的纯CSS圆角代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...