Leetcode_c++: Search in Rotated Sorted Array II (081)
2016-06-07 00:22
603 查看
题目
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.
算法
二分查找O(lgn)
class Solution { public: bool search(vector<int>& A, int target) { int n=A.size(); int left=0,right=n; while(left!=right){ int mid=(left+right)/2; if(A[mid]==target) return true; if(A[mid]>A[left]) //[left~mid] is unrotated sorted { if(A[left]<=target && target <A[mid]) right=mid; else left=mid+1; }else if(A[mid]<A[left]) // 【mid-right】 is unordered sorted { if(A[mid]<target && target<=A[right-1]) left=mid+1; else right=mid; }else left++; } return false; } };
相关文章推荐
- c++第七次上机作业
- c++第七次上机实验
- ACM:蓝桥杯:阶乘因式分解(一)
- Effective C++: {}初始化
- Leetcode_c++: Remove Duplicates from Sorted Array II (080)
- C++实验7-最大公约和和最小公倍数
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
- Leetcode_c++: Word Search (079)
- C++设计模式——单例模式
- C++ 笔记2:重载运算符
- 2016.06.06 周一 C语言 将字符串根据字符的ASCII码从大到小排序并打印出字符串中的字符类型的总数
- 地图分幅计算 之四 新旧图幅号转换及实现(C++)
- 地图分幅计算 之二 新图幅号计算及实现(C++)
- c++实验7-最大公约数与最小公倍数
- 值传递,指针传递;引用传递(c++独有)本质
- Leetcode_c++: Subsets (078)
- c++13周实验:输入/输出流的综合应用
- STL学习系列之一——标准模板库STL介绍
- C++11 std::function、std::bind
- c++:构造与析构函数基本,银行类