问题描述:
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.
原问题链接:https://leetcode.com/problems/search-in-rotated-sorted-array/
问题分析
这个问题在之前的文章里有讨论过。总的来说是基于这么一个思路。当我们将一个排序后的数组循环移位之后,其实它就构成了两个递增的段。我们用二分查找法去查找元素的时候。如果nums[mid] < nums[r],则表示它的中间节点落在后面的这个递增的段上。否则如果nums[mid] > nums[l],则可以表示成它的中间节点落在前面的这个递增段上。这个时候我们就需要对要查找的目标值和它们进行比较。
假设中间节点落在后续的递增段上,那么如果目标值target > nums[mid] 而且target < nums[r],则表示要查找的值在mid的右边,否则它就在mid的左边。对于中间点落在前面的递增段上的情况类似,如果target > nums[l] && target < nums[mid],它肯定是落在mid前面部分,否则就应该在mid后面查找。
public class Solution { public int search(int[] nums, int target) { if(nums == null || nums.length == 0) return -1; int l = 0, r = nums.length - 1; while(l <= r) { int mid = l + (r - l) / 2; if(nums[mid] == target) return mid; if(nums[mid] <= nums[r]) { if(target > nums[mid] && target <= nums[r]) l = mid + 1; else r = mid - 1; } else { if(target < nums[mid] && target >= nums[l]) r = mid - 1; else l = mid + 1; } } return -1; } }
按照这种方式可以得到元素结果的值。其算法的时间复杂度为O(logN)。
参考材料
http://shmilyaw-hotmail-com.iteye.com/blog/1626910
相关推荐
leetcode 分类 :person_running::person_running::person_running: 算法 :person_running::person_running::person_running: 实践&理论 :books: :ear: :television: Binary Search(二分查找) easy 69: 278: 35: ...
leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...
leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...
js js_leetcode题解之33-search-in-rotated-sorted-array.js
python python_leetcode题解之081_Search_in_Rotated_Sorted_Array_II
c语言入门 C语言_leetcode题解之33-search-in-rotated-sorted-array.c
c c语言_leetcode题解之0081_search_in_rotated_sorted_array_ii.zip
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
javascript js_leetcode题解之81-search-in-rotated-sorted-array-ii.js
leetcode Coding-Interview A repo for popular coding interview problems mainly from Leetcode. 二分搜索/有序数组旋转 Find Minimum In Rotated Sorted Array Find Minimum In Rotated Sorted Array II Search ...
python python_leetcode题解之153_Find_Minimum_in_Rotated_Sorted_Array.py
javascript js_leetcode题解之153-find-minimum-in-rotated-sorted-array.js
python python_leetcode题解之154_Find_Minimum_in_Rotated_Sorted_Array_II.py
javascript js_leetcode题解之154-find-minimum-in-rotated-sorted-array-ii.js
leetcode切割分组 leetcode 加减乘除运算 ...033_search_in_rotated_sorted_array.py # 旋转排序的数列中查找 034_find_first_and_last_position_of_element_in_sorted_array.py # 查找第一次出现和
- 二分查找是解决查找问题的利器,如“搜索旋转排序数组”(Search in Rotated Sorted Array)。C++的`std::lower_bound`和`std::upper_bound`函数可以简化这类问题的实现。 3. **递归与动态规划** - 递归是解决...
in Rotated Sorted Array II/Solution.java) 2014/10/20 难的 [Java](./src/在旋转排序数组中查找最小值/Solution.java) 2014/10/15 中等的 [Java](./src/最大乘积子阵列/Solution.java) 2014/9/23 中等的 [Java](./...
- Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array(在旋转排序数组中寻找最小值) - Find Minimum in Rotated Sorted Array II(在旋转排序数组中寻找...
Search in Rotated Sorted Array(搜索旋转排序数组)#数组 2020/12/08 19. Remove Nth Node From End of List(删除链表的倒数第N个节点) 153. Find Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值...