Leetcode103: Search in Rotated Sorted Array II
2015-10-17 21:15
375 查看
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
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
class Solution { public: bool search(vector<int>& nums, int target) { int n = nums.size(); if(n==0) return false; int l = 0; int r = n-1; while(l<=r) { int mid = (l+r)/2; if(nums[mid] == target) return true; else if(nums[l] < nums[mid]) { if(nums[l]<=target && target<nums[mid]) r = mid-1; else l = mid+1; } else if(nums[l] > nums[mid]) { if(nums[mid]<target && target<=nums[r]) l = mid+1; else r = mid-1; } else l++; } return false; } };
相关文章推荐
- TcxGrid导出EXCEL
- java练习
- lintcode 容易题:Compare Strings 比较字符串
- Access数据库日常维护方法(优化)
- leetcode:Basic Calculator II
- Leetcode Recover Binary Tree
- TdxAlertWindowManager右下角HINT显示控件
- 猜拳游戏的制作-使用if制作
- ZOJ 3767
- 汇编和反汇编的区别
- Python3学习笔记——基础的巩固
- 数组中求最大最小值
- simple introduction for pipe line
- Spring 通过配置向 Quartz定时任务 注入service
- pcduino里,Qt无法打开摄像头(调用了opencv)
- 三目运算符
- 好想哭
- Prime Query【ZOJ--3911】
- Chapter 5 Probabilistic Analysis and Randomized Algorithms
- 解决VMware vSphere Client与本机的鼠标无缝移出移入、剪贴板共享、共享文件夹等问题,实现虚拟机上虚拟硬件的驱动