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.
public class Solution { public int search(int[] nums, int target) { if (nums == null || nums.length == 0) { return -1; } int start = 0; int end = nums.length - 1; while (start <= end) { int mid = (start + end) / 2; if (target < nums[mid]) { if (nums[mid] < nums[end]) { end = mid - 1; } else { if (target < nums[start]) { start = mid + 1; } else { end = mid - 1; } } } else if (target > nums[mid]) { if (nums[mid] > nums[start]) { start = mid + 1; } else { if (target > nums[end]) { end = mid - 1; } else { start = mid + 1; } } } else { return mid; } } return -1; } }
相关推荐
Rotated Sorted Array Find Minimum In Rotated Sorted Array II Search In Rotated Sorted Array Search In Rotated Sorted Array II 二分搜索/有序矩阵 Kth Smallest Element In A Sorted Matrix Search A 2D ...
Search(二分查找) easy 69: 278: 35: 374: guess number higher or lower 349: intersection of two arrays 350: intersection of two arrays ii medium 33: search in sorted array 81: search in rotated sorted...
- Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array(在旋转排序数组中寻找最小值) - Find Minimum in Rotated Sorted Array II(在旋转排序数组中寻找...
Sorted Array 2 Remove Duplicates from Sorted Array II 3 Search in Rotated Sorted Array 4 Search in Rotated Sorted Array II 5 Median of Two Sorted Arrays 递归实现find kth 6 Longest Consecutive Sequence...
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
- Search in Rotated Sorted Array II: 假设按照升序排序的数组在预先未知的某个点上进行了旋转,该问题要求在旋转后的数组中搜索特定元素。 这些知识点涵盖了数据结构和算法的核心概念,以及如何用Python语言实现...
javascript js_leetcode题解之81-search-in-rotated-sorted-array-ii.js
c c语言_leetcode题解之0081_search_in_rotated_sorted_array_ii.zip
Search in Rotated Sorted Array(搜索旋转排序数组)#数组 2020/12/08 19. Remove Nth Node From End of List(删除链表的倒数第N个节点) 153. Find Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值...
颜色分类leetcode My Leetcode Problems ...Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last Position of Element in Sorted Array 在排序数组中查找元素的第一个和最后一个位
在本资源包中,主题聚焦于C语言的基础学习,特别是针对LeetCode编程挑战中的第33题——"搜索旋转排序数组"(Search in Rotated Sorted Array)。这道题目旨在检验编程者对数组处理、二分查找算法以及边界条件判断的...
leetcode 1004 leetcode E:简单,M:中等,H:...Rotated Sorted Array (M) * -> 부등호 주의, 부등호 하나 틀림 34. Find First and Last Position of Element in Sorted Array (M) 35. Search Insert Position (E)
- Search in Rotated Sorted Array(旋转排序数组中的搜索):在旋转排序数组中进行查找的问题。 - Search in a Big Sorted Array(在大型排序数组中搜索):在大量数据中进行快速搜索。 除了这些内容之外,Java...
- **2.1.3 Search in Rotated Sorted Array** - 给定一个旋转后的有序数组,找到某个元素的位置。 - 实现思路:利用二分查找法,在处理中间值时考虑数组旋转的情况。 - **2.1.4 Search in Rotated Sorted Array ...
23. **Search in Rotated Sorted Array**:在一个旋转排序数组中查找目标值。根据旋转情况,可能需要对搜索区间进行划分,使用二分查找策略。 以上就是这些题目涉及的算法和数据结构,包括但不限于哈希表、栈、队列...
比如,“搜索旋转排序数组”(Search in Rotated Sorted Array)就是一个典型的应用,数组的一部分是有序的,而另一部分是反向有序的,你需要在这样的数组中找到一个特定的元素。 五、树 树是一种非线性数据结构,...