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

原创 2015年07月08日 17:36:24

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&81. Search in Rotated Sorted Array java版(在旋转的数字中查找指定值)

33. Search in Rotated Sorted Array ,81. Search in Rotated Sorted Array II 。 leetcode解题之33&81. Search...

【LeetCode-面试算法经典-Java实现】【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】

【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Follow up fo...

【LeetCode刷题】旋转数组的查找 Search in Rotated Sorted Array

原文:http://www.dy1280.com/thread-685-1-1.html     描述 Suppose a sorted array is rotated at some pi...
  • share_4
  • share_4
  • 2014年10月06日 13:30
  • 309

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

leetcode 81. Search in Rotated Sorted Array II-旋转数组|二分查找

原题链接:81. Search in Rotated Sorted Array II 【思路-Java、Python】 二分查找实现 本题采用二分法实现,但是比较挠头的是边界问题,而且元素有重复,...

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

leetcode解题之153&154. Find Minimum in Rotated Sorted Array版(在旋转的数组中查找最小数字)

leetcode解题之153&154. Find Minimum in Rotated Sorted Array版(在旋转的数组中查找最小数字)...

[LeetCode 33]Search in Rotated Sorted Array (二分查找)

33.Search in Rotated Sorted ArraySuppose a sorted array is rotated at some pivot unknown to you befo...
  • zlasd
  • zlasd
  • 2016年10月20日 23:10
  • 146

【LeetCode】Search in Rotated Sorted Array旋转数组问题总结

// Date : 2016.09.06 // Author : yqtao // https://github.com/yqtaowhu/**************************...

【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

【033-Search in Rotated Sorted Array(在旋转数组中搜索)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Suppose a sorted...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
举报原因:
原因补充:

(最多只允许输入30个字)