新博文地址:[leetcode]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 return -1.
You may assume no duplicate exists in the array.
(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.
从一个转置排序数组中查找某个数,并返回下标,不存在则返回-1;
这道题要充分利用排序这个条件,因此可以使用折半查找,难点就在这个数组是由两个顺序数组拼接成的。
代码如下:
public int search(int[] a, int target) { int begin = 0; int end = a.length - 1; while (begin <= end) { int mid = (begin + end) >> 1; // make sure where middle element locates if (a[mid] == target) { return mid; } else if (target > a[mid]) { if (a[mid] > a[end]) {// 说明mid在前半段,那么target肯定在前半段 begin = mid + 1; } else if (a[mid] < a[begin]) {// 说明mid在后半段,要么target在前半段,要么target在后半段 if(target > a[end]){//target 在前半段 end = mid - 1; }else if(target < a[begin]){//target在后半段 begin = mid + 1; } } else {// 说明begin、end已经锁定到递增的序列里面了 begin = mid + 1; } } else {//target比mid小 if (a[mid] < a[begin]) {// 如果mid在后半段,说明target必须在后半段 end = mid - 1; } else if (a[mid] > a[end]) {// mid在前半段 if(target > a[end]){//target在前半段 end = mid - 1; }else if(target < a[begin]){//target在后半段 begin = mid + 1; } } else {// 说明begin、end已经锁定到递增的序列里面了 end = mid - 1; } } } return -1; }
将代码化简之后是这样的:
public int search(int[] a, int target) { int begin = 0; int end = a.length - 1; while (begin <= end) { int mid = (begin + end) >> 1; if (a[mid] == target) { return mid; } else if (target > a[mid]) { if (a[mid] < a[begin] && target > a[end]) { end = mid - 1; } else { begin = mid + 1; } } else { if (a[mid] > a[end] && target < a[begin]) { begin = mid + 1; } else { end = mid - 1; } } } return -1; }
相关推荐
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
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 分类 :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 ...
- 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(寻找旋转排序数组中的最小值...
Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. ...
颜色分类leetcode My Leetcode Problems ...Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last Position of Element in Sorted Array 在排序数组中查找元素的第一个和最后一个位
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
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)