Leetcode Search in Rotated Sorted Array II
来源:互联网 发布:linux新建文件夹命令 编辑:程序博客网 时间:2024/06/08 17:57
Suppose an array sorted in ascending order 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
).
Write a function to determine if a given target is in the array.
The array may contain duplicates.
代码如下:
class Solution {public: bool search(vector<int>& nums, int target) { int left = 0,right = nums.size()-1,mid; while(left <=right) { mid = (left+right)>>1; if(nums[mid] == target) return true; if(nums[left] < nums[mid]) { if(nums[left] <= target && nums[mid] > target) { right = mid -1; } else { left = mid+1; } } else if(nums[left] > nums[mid]) { if(nums[left] > target && nums[mid] <target) { left = mid + 1; } else { right = mid - 1; } } else { left++; } } 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
- HttpUrlConnection与HttpClient的认识(四) -HttpClient的封装
- 【Junit】The import org.junit.Test conflicts with a type defined in the same file报错
- [IOS APP]西藏生死书
- 学习资料
- Could not find result map 错误
- Leetcode Search in Rotated Sorted Array II
- 正被停用的激活上下文不是最近激活的
- jdk源码解读-并发包-Lock-ReentrantReadWriteLock(1)-整体介绍以及读锁的lock 和 unlock 解析
- 图像归一化作用和方法
- 使用scikit-learn进行文本分类
- mongodb update 重命名列
- Spring MVC 入门示例讲解
- 最优二叉搜索树自底向上非递归的动态规划算法
- Webview 全面详解