zoukankan      html  css  js  c++  java
  • 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 may assume no duplicate exists in the array.

    Your algorithm's runtime complexity must be in the order of O(log n).

    Example 1:

    Input: nums = [4,5,6,7,0,1,2], target = 0
    Output: 4
    

    Example 2:

    Input: nums = [4,5,6,7,0,1,2], target = 3
    Output: -1

    this is my first code, it can't satisfy the last condition:

    class Solution {
    public:
        int search(vector<int>& nums, int target) {
            int len = nums.size();
            if (len == 0) return -1;
            bool flag = false;
            if (target < nums[0]) {
                for (int i = len-1; i > 0; --i) {
                    if (nums[i] == target) {
                        flag = true;
                        return i;
                    } 
                }
            } else {
                for (int i = 0; i < len; ++i) {
                    if (nums[i] == target) {
                        flag = true;
                        return i;
                    }
                        
                }
            }
            if (!flag) {
                return -1;
            }
        }
    };

    this is the right way to use binary search:

    class Solution {
    public:
        int search(vector<int>& nums, int target) {
            int len = nums.size();
            if (len == 0) return -1;
            int l = 0; 
            int r = len - 1;
            while (l <= r) {
                int m = (l + r) / 2;
                if (nums[m] == target) return m;
                if (nums[m] > nums[r]) {
                    if (target < nums[m] && target >= nums[l]) {
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                } else if (nums[m] < nums[l]) {
                    if (target <= nums[r] && target > nums[m]) {
                        l = m + 1;
                    } else {
                        r = m -1;
                    }
                } else {
                    if (target < nums[m]) {
                        r = m -1;
                    } else {
                        l = m + 1;
                    }
                }
            }
            return -1;
        }
    };
    

    Runtime: 8 ms, faster than 25.99% of C++ online submissions for Search in Rotated Sorted Array.

    but this way cost more time before the previous way.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    PHP 进制汉字转化
    当调用方法没有注释信息并且参数不全
    DBCP数据库连接池技术的两种实现方式
    汇编语言中一步执行循环
    求最小函数依赖集
    汇编语言实验四
    汇编语言第七章
    batch实现数据库的批量插入Unknown system variable 'query_cache_size'
    汇编语言第一节课:数制转换,真值和补码
    Forsaken喜欢数论
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9775497.html
Copyright © 2011-2022 走看看