leetcode--Search in Rotated Sorted Array II
来源:互联网 发布:一个网络协议主要由 编辑:程序博客网 时间:2024/05/22 10:24
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.
题意:和leetcode--Search in Rotated Sorted Array一样,但是数组中可能出现重复元素
分类:数组,二分法
解法1:如果遇到无法判断在哪边的元素,说明是重复元素,这时只能遍历查找了
public class Solution { public boolean search(int[] nums, int target) { int low = 0; int high = nums.length-1; while(low<=high){ int mid = (low+high)/2; if(nums[mid]==target) return true; if(nums[mid]>nums[low]){//如果mid属于左边 if(nums[mid]>target && nums[low]<=target){//如果target属于左边 high = mid-1; }else{ low = mid+1; } }else if(nums[mid]<nums[low]){//如果mid属于右边 if(target>nums[mid] && target<=nums[high]){ low = mid+1; }else{ high = mid-1; } }else{ for(int i=low;i<=mid;i++){ if(nums[i]==target) return true; } low = mid+1; } } return false; }}
0 0
- 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
- [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
- 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
- 【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
- LeetCode | Search in Rotated Sorted Array II
- LeetCode Search in Rotated Sorted Array II
- jsp 有哪些动作?作用分别是什么?
- python igraph whl文件的安装
- tomcat详解
- GRE满分作文(北美范文)
- (转)Linux进程池的实现
- leetcode--Search in Rotated Sorted Array II
- [数学]Sum of f(x) 约数之和问题
- AppDelegate生命周期详解
- 面试常考的atoi函数
- jsp 有哪些内置对象?作用分别是什么?
- 决策树的Python代码分析(一)
- Minimum Depth of Binary Tree
- Spring Bean的5种作用域
- MVVM