[LeetCode] Search in Rotated 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.
思路:本题思路和http://www.cnblogs.com/vincently/p/4122528.html类似。只是如果有重复的话就不能依靠与边界的比较确定递增的序列。例如,[1,3,1,1,1],对于A[m]>=A[l]来说,[l,m]的假设就不成立。但是还是可以观察到,左边子数组的值都要大于等于右边子数组的值。将A[m]>=A[l]拆分为两种情况:A[m]>A[l],则区间[l,m]一定递增;如果 A[m]==A[l]确定不了,那就l++,往下看一步。
时间复杂度改变为O(n),空间复杂度O(1)
class Solution {
public:
bool search(int A[], int n, int target) {
int first = , last = n - ;
while (first <= last) {
const int mid = low + ((high - low) >> 1);
if (A[mid] == target) return true;
if (A[first] < A[mid]) {
if (A[first] <= target && target < A[mid])
last = mid - ;
else
first = mid + ;
} else if (A[first] > A[mid]){
if (A[mid] < target && target <= A[last])
first = mid + ;
else
last = mid - ;
} else {
first++;
}
}
return false;
}
};
[LeetCode] Search in Rotated Array II的更多相关文章
- LeetCode(81) Search in Rotated Array II
题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- [LeetCode] Search in Rotated Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
随机推荐
- Creating Isomorphic Apps with Node.js, React, and Express
In this article, we’re going to use following software: React: the UI framework that can rendered on ...
- 实验楼学习linux第一章第三节用户及文件权限管理
用户及文件权限管理 常用命令 查看用户 whoami 创建用户 sudo adduser 用户名 切换账户 su 用户名 删除账户 sudo deluser 用户名 --remove-home 查看用 ...
- 2016-2017-2 20155227实验三《敏捷开发与XP实践》实验报告
2016-2017-2 20155227实验三<敏捷开发与XP实践>实验报告 实验内容 一.实验内容 XP基础 XP核心实践 相关工具 二.实验过程 (一)敏捷开发与XP 1.XP是以开发 ...
- 20155230 2016-2017-2《Java程序设计》第二周学习总结
20155230 2016-2017-2 <Java程序设计>第er周学习总结 教材学习内容总结 JAVA编程风格 1.命名变量时不可以使用数字及特殊字符作为开头. 2.变量名称不可以与J ...
- 20155307《Java程序设计》实验二实验报告
一.单元测试和TDD 用程序解决问题时,要学会写以下三种代码: 伪代码 产品代码 测试代码 正确的顺序应为:伪代码(思路)→ 测试代码(产品预期功能)→ 产品代码(实现预期功能),这种开发方法叫&qu ...
- 【转载】GC基本算法及C++GC机制
原文: GC基本算法及C++GC机制 阅读目录 前言 基本概念 有向可达图与根集 三种基本的垃圾收集算法及其改进算法 1.引用计数算法 2. Mark & Sweep 算法 3. 节点复制算法 ...
- day 9 追踪一个蓝色的物体
# -*- coding: utf- -*- import cv2 import numpy as np #.打开摄像头 cap=cv2.VideoCapture('output.avi') ): # ...
- 深入理解C++中的Const,Mutable以及Volatile
我一直认为const表示一个常量,常量就是一个无法被修改的值,但是没有深入理解const的实现,甚至不知道mutable和volatile的存在,最近在书中看到了这一部分的知识,所以本文将详细解析这几 ...
- katalon系列十五:给浏览器添加cookie自动登陆
import org.openqa.selenium.Cookieimport org.openqa.selenium.WebDriverimport com.kms.katalon.core.web ...
- 抓包工具Charles学习总结
最近由于工作需要对App进行测试,功能方面还好说,但是在网络测试方面遇到了一些问题.由于公司App是使用https进行通信,直接在路由器上抓包下来,数据包都是加密的,没法看到接口返回的内容,给测试的B ...