[LeetCode]题解(python):033-Search in Rotated Sorted Array

题目来源:

  https://leetcode.com/problems/search-in-rotated-sorted-array/



题意分析:

  在一个翻转数组实现一个查找。(什么叫翻转数组,也就是,原来排好序的数组,选择一个点,将这个点之前的数放到数组的后面,不如4,5,6,7,1,2,3就是一个翻转数组)。



题目思路:

  这道题目的思路和二分查找的思路相似,难度主要是在确定二分的位置。



代码(python):

  

 1 class Solution(object):
 2     def search(self, nums, target):
 3         """
 4         :type nums: List[int]
 5         :type target: int
 6         :rtype: int
 7         """
 8         size = len(nums)
 9         first = 0;last = size
10         while first != last:
11             mid = (first + last) // 2
12             if nums[mid] == target:
13                 return mid
14             if nums[first] <= nums[mid]:
15                 if nums[first] <= target and target < nums[mid]:
16                     last = mid
17                 else:
18                     first = mid + 1
19             else:
20                 if nums[mid] < target and target <= nums[last - 1]:
21                     first = mid + 1
22                 else:
23                     last = mid
24         return -1



转载请注明出处:http://www.cnblogs.com/chruny/p/4918379.html

时间: 2024-11-15 02:24:31

[LeetCode]题解(python):033-Search in Rotated Sorted Array的相关文章

[LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. Search in Rotated Sorted Array (Hard) 链接: 题目:https://leetcode.com/problems/search-in-rotated-sorted-array/ 代码(github):https://github.com/illuz/leetcode 题

LeetCode 033 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

[Leetcode][Python]33: Search in Rotated Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 33: Search in Rotated Sorted Arrayhttps://oj.leetcode.com/problems/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

Java for LeetCode 033 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第32题--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(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 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 dup

033. Search in Rotated Sorted Array

1 class Solution { 2 public: 3 int search(vector<int>& nums, int target) { 4 if (nums.size() == 0) return -1; 5 else { 6 int left = 0, right = nums.size() - 1; 7 while (left <= right) { 8 int mid = left + (right - left) / 2; 9 //cout <<

LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 . 你可以假设数组中不存在重复的元素. 你的算法时间复杂度必须是 O(log n) 级别. 英文:Suppose an array sorted i

Java for LeetCode 081 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. 解题思路: 参考Java for LeetCode 033 Search in Rota

[LeetCode]题解(python):081 - Search in Rotated Sorted Array II

题目来源 https://leetcode.com/problems/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