[LeetCode] Rotated Sorted Array II

 1 public class Solution {
 2     public boolean search(int[] A, int target) {
 3         int low=0, high=A.length-1;
 4         while (low<high) {
 5             int mid=(low+high)/2;
 6             if (A[low]==A[mid] && A[mid]==A[high]) {
 7                 low++;
 8                 high--;
 9             } else if (A[mid]<=A[high]) {
10                 if (A[mid]<target && A[high]>=target) low=mid+1;
11                 else high=mid;
12             } else if (A[low]<=A[mid]) {
13                 if (A[low]<=target && A[mid]>=target) high=mid;
14                 else low=mid+1;
15             }
16         }
17
18         if (A[low]==target) return true;
19         else return false;
20     }
21 }
时间: 2024-08-10 23:12:59

[LeetCode] Rotated Sorted Array II的相关文章

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: 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. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: 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 arr

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/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

[LeetCode] Search in Rotated Sorted Array II [36]

题目 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. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted

LeetCode OJ: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. 这个和前面的一个不一样就在于可能会有重复的数字,那么判断的时候就应该注意了,遇到start

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/search

LeetCode: Search in Rotated Sorted Array II [081]

[题目] 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. [题意] 在"Search in Rotated Sorted Ar

leetcode -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

leetcode第一刷_Search in Rotated Sorted Array II

接着上一篇,同样是旋转数组中查找问题.如果这个数组有重复元素怎么办呢?会有什么影响? 我举一个极端的例子,假设数组中的元素是这样的,1,1,2,1,1,1,1,我们要在这个数组中查找2,一开始的A[middle]=1,发现比target小,那我们就看看A[0]和A[N],发现都跟A[middle]相等,那么这个2到底在哪一半中?只有上帝知道,如果他老人家真的存在的话.这种时候我们怎么办呢?没有其他的办法,只能从头开始乖乖的扫描,直到发现target或者确定他不存在. 为什么会出现这种情况,或者说