zoukankan      html  css  js  c++  java
  • search in rotated sorted array

    descripte problem:

      there is an array,at the first it is sorted,but for som reason, it was rotated at some pivot unknown to you beforehand.then you should find the target num in the array,and return the index

    i.e:

    [4,5,6,1,2,3]

    [1]

    return 

    4

    solution:

      you should treat the array as two aescend array,which named part1,part2.Then you can use the binary search to find the low ,high and middle.

      first, calculate the middle = (low +high)/2

      second,if nums[middle] == target, then middle is the index which we find ;else judge which part is the middle belonged to, part1 or part2

      third, if the middle belong to part1, you should know that whether nums[low] <= target <= nums[middle], if ture it turns out that target must exits between nums[low] and nums[middle],so you just need make high = middele -1;if it is false, the target is impossible to exits between nums[low] and nums[middle], so you just need make low = middle +1.

      fourth, if the middle belong to part2, you have to find if nums[middle] <= target <= nums[high], if ture it turns  out that target must exits between nums[middle] and nums[high], so you just need make low = middle +1

    ;if false, you should make high = middle - 1

    there is my python code

    class Solution(object):
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            low = 0
            high = len(nums)-1
            while low<=high:
                middle = (low+high)/2
                if nums[middle]==target:
                    return middle
                if nums[middle] >= nums[low]:
                    if target < nums[middle] and target >= nums[low]:
                        high = middle - 1
                    else:
                        low = middle + 1
                else:
                    if target <= nums[high] and target >= nums[middle]:
                        low = middle + 1
                    else:
                        high = middle - 1
                
            return -1
                

    of course, you can just use the order search, but it has be proved that the exceed time is not stable

    there is the simple solution

    class Solution(object):
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            for i in range(len(nums)):
                if nums[i] == target:
                    return i
            return -1
  • 相关阅读:
    C# 6.0:在catch和finally中使用await
    C# 6.0:Expression – Bodied Methods
    C# 6.0:Auto-Property initializer
    C# 6.0:String Interpolation
    【转】http://www.cnblogs.com/yuzukwok/p/3884377.html
    ThoughtWorks面试题(标记给自己看)
    C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
    (转)C#编程中的66个好习惯
    (转)解决WINDOWS 程序界面闪烁问题的一些经验
    C#利用反射动态绑定事件
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6755388.html
Copyright © 2011-2022 走看看