【Search In Rotated Sorted Array】cpp

题目

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.

代码

class Solution {
public:
    int search(int A[], int n, int target) {
        int begin = 0;
        int end = n-1;
        while (begin != end)
        {
            if( begin+1 == end )
            {
                if (A[begin]==target) return begin;
                if (A[end]==target) return end;
                return -1;
            }
            const int mid = (end+begin)/2;
            if (A[mid]==target) return mid;
            if(target<A[mid])
            {
                if(A[begin]<A[mid])
                {
                    if(target>=A[begin])
                    {
                        end = mid-1;
                    }
                    else
                    {
                        begin = mid+1;
                    }
                }
                else
                {
                    end = mid-1;
                }
            }
            else
            {
                if(A[begin]<A[mid])
                {
                    begin = mid+1;
                }
                else
                {
                    if(target<=A[end])
                    {
                        begin = mid+1;
                    }
                    else
                    {
                        end = mid-1;
                    }
                }
            }
        }
        if (A[begin]==target) return begin;
        return -1;
    }
};

Tips:

1. 分target与A[mid]大小情况先讨论

2. 由于前半截或后半截至少一个是有序的,再按照这个来分条件讨论

if else代码中有一些逻辑可以合并,但是考虑到保留原始逻辑更容易被理解,就保留现状了

时间: 2024-10-10 03:51:42

【Search In Rotated Sorted Array】cpp的相关文章

leetcode 【 Search in Rotated Sorted Array 】python 实现

题目: 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

【Remove Duplicates from Sorted Array】cpp

题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do

leetcode 【 Search in Rotated Sorted Array II 】python 实现

题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/4254590.html 代码:oj测试通过 Runtime: 73 ms 1 class Solution: 2 # @param A a list of integers 3 # @param target an integer 4 # @return a boolean 5 def search(sel

62. Search in Rotated Sorted Array【medium】

62. Search in Rotated Sorted Array[medium] 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, ot

【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 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. [解析] 相比Search in Rotated Sorted Array,在

【leetcode刷题笔记】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. 题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半.但是如果有

【leetcode】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. 与I类似,只是在二分搜

【leetcode】Search in Rotated Sorted Array

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 retu