LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
给出排序好的一维有重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数是否存在,时间复杂度限制\(O(log_2n)\)
C++
因为有重复元素存在,nums[l] <= nums[mid]不能说明[l,mid]区间内一定是单调的,比如数组[1,2,3,1,1,1,1],但是严格小于和严格大于的情况还是可以判断的。所以当nums[l] == nums[mid]时,令l++,跳过这个重复元素。所以其实该算法最坏情况下(所有元素相同)时间复杂度是\(O(n)\)的。
class Solution {
public:
bool search(std::vector<int>& nums, int target) {
int l = 0,r = nums.size();
while (l<r){
const int mid = l + (r-l)/2;
if(nums[mid] == target) return true;
if(nums[l] < nums[mid]) // [l,mid]区间单调不降
if(nums[l] <= target && target < nums[mid]) r = mid; // target 在区间内
else l = mid + 1; // target 不在区间内
else if(nums[mid] < nums[l]) // [mid,r]区间单调不降
if(nums[mid] < target && target <= nums[r-1]) l = mid + 1; // target 在区间内
else r = mid; // target 不在区间内
else l++; //跳过这个重复元素
}
return false;
}
};
Java
Python3
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>的更多相关文章
- 154. Find Minimum in Rotated Sorted Array II(Binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...
- 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 ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 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 OJ: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
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 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
随机推荐
- twfont
module game { /** *Created by 渔歌烟火 on 2018/3/28. * 字体缓动动画 */ export class LabelEffect { private stat ...
- 小E浅谈丨区块链治理真的是一个设计问题吗?
在2018年6月28日Zcon0论坛上,“区块链治理”这个话题掀起了大神们对未来区块链治理和区块链发展的一系列的畅想. (从左至右,分别为:Valkenburgh,Zooko,Jill, Vitali ...
- Python爬虫之12306-买票器小白源码
研究不易 import requests import re import urllib.parse import json import datetime from collections impo ...
- centos6.8编译安装mysql
1.安装编译代码需要的包 yum -y install make gcc-c++ cmake bison-devel ncurses-devel 2.创建mysql用户(但是不能使用mysql账号登陆 ...
- MySQL存储过程(PROCEDURE)(一)
一.定义与目的: 定义:存储过程是数据库 SQL 语言层面的代码封装与重用(是数据库中存储复杂程序,以便外部程序调用的一种数据库对象): 目的:我们为了完成特定功能的SQL语句集,经编译创建并保存在数 ...
- 论文阅读笔记五十一:CenterNet: Keypoint Triplets for Object Detection(CVPR2019)
论文链接:https://arxiv.org/abs/1904.08189 github:https://github.com/Duankaiwen/CenterNet 摘要 目标检测中,基于关键点的 ...
- JsonIgnore注解不起作用的解决办法
一开始在属性上注解了JsonIgnore以为就不会序列化了,结果还是有这个属性,看来是没有起作用啊 [JsonIgnore] public List<int> SubjectAndSubS ...
- [原创][Synth 8-2543] port connections cannot be mixed ordered and named ["*_Top.v":1151]
Vivado综合错误: [Synth 8-2543] port connections cannot be mixed ordered and named ["*_Top.v":1 ...
- Python 实现整数线性规划:分枝定界法(Branch and Bound)
今天做作业,要实现整数线性规划的分枝定界法算法.找了一些网上的博客,发现都很屎,感觉自己写的这个比较清楚.规范,所以在此记录.如有错误,请指正. from scipy.optimize import ...
- python全栈开发day117-MongoDB,pymongo
1.MongoDB操作 使用了不存在的对象即创建该对象 1.增加: 官方不推荐写法: insert([{},{},{}]) 官方推荐写法: insertOne({}) insertMany([{},{ ...