LeetCode *** 81. Search in Rotated Sorted Array II
2016-04-15 10:43
267 查看
题目:
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.
分析:
我最近头很晕感觉不在状态。。。
代码:
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.
分析:
我最近头很晕感觉不在状态。。。
代码:
class Solution { public: bool search(vector<int>& nums, int target) { if(nums.size()<1)return false; int low=0,high=nums.size()-1,mid; while(low<high){ mid=(low+high)/2; if(nums[mid]==target)return true; if(nums[mid]>nums[high]){ if(nums[mid]>target&&nums[low]<=target)high=mid; else low=mid+1; } else if(nums[mid]<nums[high]){ if(nums[mid]<target&&nums[high]>=target)low=mid+1; else high=mid; }else high--; } return nums[low]==target; } };
相关文章推荐
- 主线程和子线程的合理使用03
- 产品需求文档(PRD)的写作方法之笔记一
- java读取CSV文件添加到sqlserver数据库
- gdb调试技巧
- 软件工程---相互观看与评价
- 【软件工程】复利计算器--结对编程3.0评论博客
- AE开发能否实现TOC Control里添加多个Data Frame
- 表单事件,onblur,onfocus,焦点
- 链表
- Centos 6.5 安装oracle
- SpatialHadoop:有效的分析你的空间数据
- java中的23种设计模式
- 2016.04.15
- TP下的session.memcache
- Hash概率问题
- Java技术晋升之路
- jQuery冒泡事件阻止
- 复利计算器评论
- Linux使用笔记
- 二叉树层次遍历