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.
class Solution { public: int search(int A[], int n, int target) { if (A == NULL || n <= 0) return false; int first = find_first(A, n); int left = first, right = first + n - 1; int *a = A; while (right - left > 1) { int mid = left + (right - left) / 2; if (a[mid%n] == target) return mid%n; else if (a[mid%n] > target) right = mid; else left = mid; } if (a[right%n] == target) return right %n; if (a[left%n] == target) return left%n; return -1; } int find_first(int *a, int n) { if (a == NULL || n <= 0) return -1; int left = 0, right = n - 1; while (right - left > 1) { int mid = left + (right - left) / 2; if (a[mid] > a[right]) left = mid; else if (a[mid] < a[left]) right = mid; else break; } if (left < right) { return (a[left] < a[right] ? left : right); } else return -1; } };
Search in Rotated Sorted Array IIApr 20 '124077 / 10753
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.
class Solution { public: bool search(int A[], int n, int target) { int s = find1st(A, n, target); int l = s, r = s + n - 1; auto a = A; while (l < r) { if (r - l == 1) break; int mid = (r + l) / 2; if (a[mid%n] <= target) l = mid; else if (a[mid%n] > target) r = mid; } if (a[l%n] == target || a[r%n] == target) return true; else return false; } int find1st(int A[], int n, int target) { int l = 0, r = n - 1; auto a = A; while (l < r && a[l] == a[r]) l++; if (a[l] < a[r]) return l; while (l < r) { if (r - l == 1) break; int mid = (r + l) / 2; if (a[mid] == a[l]) l = mid; else if (a[mid] > a[l]) l = mid; else if (a[mid] < a[l]) r = mid; } if (a[l] < a[r]) return l; else return r; } };
相关推荐
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
- Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array(在旋转排序数组中寻找最小值) - Find Minimum in Rotated Sorted Array II(在旋转排序数组中寻找...
leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...
leetcode 分类 :person_running::person_running::person_running: 算法 :person_running::person_running::person_running: 实践&理论 :books: :ear: :television: Binary Search(二分查找) easy 69: 278: 35: ...
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. ...
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](./...
颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
##### 2.1.3 Search in Rotated Sorted Array **题目描述**:给定一个按非递减顺序排序后旋转得到的数组,找到某个特定目标值的索引位置。若不存在,则返回 `-1`。 **解题思路**: 1. 使用二分查找法,关键在于...