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.
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.
Search in Rotated Sorted Array的进化,考虑了有重复数字的情况,在 Search in Rotated Sorted Array中,可以比较容易的判断target与a[mid]之间的位置关系,从而决定该搜索哪半边,但是加入重复数字的情况后,情况变得略微复杂了。
例如[4,4,4,4,4,5,3,4,4],target = 3
第一次得到的a[mid] = 4
如果序列数字都是唯一的,像 Search in Rotated Sorted Array中的情况,我们可以很容易的判断出4在那边,并只需要搜索相应的半边就可以了。
但是考虑了重复,前半段序列和后半段序列出现了交集,我们需要判断一下这个mid是属于前半段的还是后半段的。
值得庆幸的是,target不需要判断,比如target = 4,因为只是为了证明存在性,因此我们可以假设是任意一个4既可以是前半段的4,也可以是后半段的4。
public boolean search(int[] a,int target){ if(a == null || a.length == 0){ return false; } int begin = 0; int end = a.length -1; while(begin <= end){ int mid = (begin + end)>>1; if(a[mid] == target){ return true; }else if(a[mid] < target){ //mid在后半段,target在前半段时 if(findPlace(a, begin, end, mid) == -1 && target > a[end]){ end = mid - 1; }else{ begin = mid + 1; } }else{ //mid在前半段,target在后半段时 if(findPlace(a, begin, end, mid) == 1 && target < a[begin]){ begin = mid + 1; }else{ end = mid - 1; } } } return false; } private int findPlace(int[] a,int begin,int end,int index){ //1表示在前半段,-1表示在后半段,0表示順序序列 if(a[index] < a[begin] ){ return -1; } if(a[index] == a[begin]){ for(int i = index; i >= begin; i--){ if(a[i] != a[begin]){ return -1; } } return 1; } if(a[index] > a[end]){ return 1; } if(a[index] == a[end]){ for(int i = index; i <= end; i++){ if(a[i] != a[end]){ return 1; } } return -1; } return 0; }
相关推荐
python python_leetcode题解之081_Search_in_Rotated_Sorted_Array_II
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 ...
js js_leetcode题解之33-search-in-rotated-sorted-array.js
c语言入门 C语言_leetcode题解之33-search-in-rotated-sorted-array.c
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 ...
python python_leetcode题解之153_Find_Minimum_in_Rotated_Sorted_Array.py
javascript js_leetcode题解之153-find-minimum-in-rotated-sorted-array.js
- Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array(在旋转排序数组中寻找最小值) - Find Minimum in Rotated Sorted Array II(在旋转排序数组中寻找...
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
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 Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...
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)...