Search in Rotated Sorted Array, 查找反转有序序列。利用二分查找的思想。反转序列。

问题描述:一个有序序列经过反转,得到一个新的序列,查找新序列的某个元素。12345->45123。

算法思想:利用二分查找的思想,都是把要找的目标元素限制在一个小范围的有序序列中。这个题和二分查找的区别是,序列经过mid拆分后,是一个非连续的序列。特别要注意target的上下限问题。因为是非连续,所以要考虑上下限,而二分查找,序列式连续的,只用考虑单限。有递归算法和迭代算法。

递归算法:

 1 public int search(int[] nums, int target)
 2     {
 3         return binarySearch(nums, 0, nums.length - 1, target);
 4     }
 5     //递归方法
 6     public int binarySearch(int[] nums, int left, int right, int target)
 7     {
 8         //不要忘了这个边界条件。
 9         if(left > right)
10         {
11             return -1;
12         }
13         int mid = (left + right)/2;
14         if(target == nums[mid])
15         {
16             return mid;
17         }
18         if(nums[left] <= nums[mid])//做连续,要包含"="的情况,否则出错。
19         {
20             if(target >= nums[left] && target < nums[mid])//target上下限都要有
21             {
22                 return binarySearch(nums, left, mid - 1, target);//记得return
23             }
24             else
25             {
26                 return binarySearch(nums, mid + 1, right, target);
27             }
28         }
29         else
30         {
31             if(target > nums[mid] && target <= nums[right])
32             {
33                 return binarySearch(nums, mid + 1, right, target);
34             }
35             else
36             {
37                 return binarySearch(nums, left, mid - 1, target);
38             }
39         }
40     }

迭代算法:

//迭代方法
    public int binarySearch2(int[] nums, int left, int right, int target)
    {

        while(left <= right)
        {
            int mid = (left + right)/2;
            if(target == nums[mid])
            {
                return mid;
            }

            if(nums[left] <= nums[mid]) //左连续,所以要包含=的情况。否则出错。
            {
                if(target >= nums[left] && target < nums[mid])
                {
                    right = mid - 1;
                }
                else
                {
                    left = mid + 1;
                }
            }
            else
            {
                if(target > nums[mid] && target <= nums[right])
                {
                    left = mid + 1;
                }
                else
                {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }
时间: 2024-08-08 16:35:32

Search in Rotated Sorted Array, 查找反转有序序列。利用二分查找的思想。反转序列。的相关文章

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. EXAMPLE Input: find 5 in {15, 16, 19, 20, 2

LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 这个和前面的一个不一样就在于可能会有重复的数字,那么判断的时候就应该注意了,遇到start

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

[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 7might become4 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

leetcode 7. 在有序可重复数组旋转后搜索 Search in Rotated Sorted Array II

Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 本题基于

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

leetcode 6. 在有序数组旋转后搜索 Search in Rotated Sorted Array

Search in Rotated Sorted Array 难度:Hard 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, otherw

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

leetcode 二分查找 Search in Rotated Sorted Array

Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions 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 s