leetcode_Search in Rotated Sorted Array II
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(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(A == NULL || n <) return false; int left = ;
int right = n-;
while(left <= right){ int mid = (right - left)/ + left;
if(A[mid] == target)
return true;
if(A[mid] > A[left])
{
if(target >= A[left] && target < A[mid])
right = mid - ;
else
left = mid + ;
}else if(A[mid] < A[left]){ if(target <= A[right] && target > A[mid])
left = mid + ;
else
right = mid -;
}else{
if(A[left] == target)
return true; ++left ;
}
}
return false;
}
};
leetcode_Search in Rotated Sorted Array II的更多相关文章
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
随机推荐
- hdu2444The Accomodation of Students
思路: 二分图判断+最大匹配模板 二分图判断的方法很好想,没有离散的基础凭空给你个图让你判断也很容易想到染色法,简单的介绍下就是用queue来做,标记一个点为x则他所有的邻点都为x',然后递归的执行下 ...
- cdh4.1.2 hadoop和oozie集成问题
1.异常信息例如以下: Caused by: com.google.protobuf.ServiceException: java.net.ConnectException: Call From sl ...
- qt 实现钟表图标
#include "clock.h" CLOCK::CLOCK(QWidget *parent) : QWidget(parent) { QTimer *timer = new Q ...
- python实现websocket服务器,可以在web实时显示远程服务器日志
一.开始的话 使用python简单的实现websocket服务器,可以在浏览器上实时显示远程服务器的日志信息. 之前做了一个web版的发布系统,但没实现在线看日志,每次发布版本后,都需要登录到服务器上 ...
- C语言之可重入函数 && 不可重入函数
可重入函数 在 实时系统的设计中,经常会出现多个任务调用同一个函数的情况.如果这个函数不幸被设计成为不可重入的函数的话,那么不同任务调用这个函数时可能修改其他任 务调用这个函数的数据,从而导致不可预料 ...
- 强制转https
原文:http://blog.csdn.net/wzy_1988/article/details/8549290 需求简介 基于nginx搭建了一个https访问的虚拟主机,监听的域名是test.co ...
- Java 8十个lambda表达式案例
1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnable() { @Override public ...
- css伪类选择器详细解析及案例使用-----伪类选择器(1)
动态伪类选择器:E:link :选择匹配的E元素,并且匹配元素被定义了超链接并未被访问过.E:visited :选择匹配的E元素,而且匹配的元素被定义了连接并已被访问过.E:active :选择匹配的 ...
- SqlServer死锁与阻塞检测脚本
IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'sp_Lock_Scan') DROP PROCEDURE sp_Lock_Scan GO CR ...
- 武汉科技大学ACM :1005: 零起点学算法101——手机短号
Problem Description 大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号.假设所有的短号都是是 6+手机号的后5位,比 ...