Search in Rotated Sorted Array II
来源:互联网 发布:php找工作去哪里好 编辑:程序博客网 时间:2024/06/09 04:13
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.
public class Solution { public boolean search(int[] A, int target) { if(A==null || A.length<=0) return false; int index1=0; int index2=A.length; int midIndex=index1; //if(A.length==1 && A[0]==target) return true; while(index1!=index2){ midIndex=index1+(index2-index1)/2; if(A[midIndex]==target) return true; else if(A[index1]<A[midIndex]){ if(A[index1]<=target && target<A[midIndex]){ index2=midIndex; } else{ index1=midIndex+1; } } else if(A[index1]>A[midIndex]){ if(A[midIndex]<=target && target<=A[index2-1]){ index1=midIndex+1; } else{ index2=midIndex; } } else{ //skip duplicate elements index1++; } } return false; }}
0 0
- LeetCode: Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- LeetCode Search in Rotated Sorted Array II
- LeetCode : Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II
- [Leetcode] Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- [leetcode] Search in Rotated Sorted Array II
- [LeetCode]Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- [leetcode] Search in Rotated Sorted Array II
- LeetCode-Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- LeetCode - Search in Rotated Sorted Array II
- 【Leetcode】Search in Rotated Sorted Array II
- Java HashMap 分析之二:Hash code
- 二分图的最大匹配
- Java HashMap 分析之三:放入元素
- 【索引】Volume 1. Elementary Problem Solving
- FSG报表打印报错,log文件显示java.sql.SQLException: No corresponding LOB data found
- Search in Rotated Sorted Array II
- Java HashMap 分析之四:查找和内存使用
- Android内存优化
- 杭电 1850(尼姆博弈)
- 工具安装
- csdn的感悟
- 当传递具有已删除行的 DataRow 集合时,更新要求有效的 DeleteCommand。
- 二分图的最大匹配
- 去掉“正在更新IntelliSense”