leetcode68-search-in-rotated-sorted-array-ii
题目描述
继续思考题目 "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.
输出
true
class Solution {
public:
/**
*
* @param A int整型一维数组
* @param n int A数组长度
* @param target int整型
* @return bool布尔型
*/
bool search(int* A, int n, int target) {
// write code here
for (int i=0;i<n;i++){
if (A[i]==target)
return true;
}
return false;
}
};
class Solution {
public:
/**
*
* @param A int整型一维数组
* @param n int A数组长度
* @param target int整型
* @return bool布尔型
*/
bool search(int* A, int n, int target) {
// write code here
int first=0;
int last=n-1;
while (first<=last){
int mid=first+(last-first)/2;
if (A[mid]==target)
return true;
if (A[first]==A[mid]&&A[mid]==A[last]){
first++;
last--;
}
else if (A[first]<=A[mid])
{
if (A[first]<=target && target<A[mid])
last=mid-1;
else
first=mid+1;
}
else if (A[mid]<=A[last]){
if (A[mid]<target && target <=A[last])
first=mid+1;
else
last=mid-1;
}
}
return false;
}
};
leetcode68-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 ...
- 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】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 ...
- 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 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- ISP-OB, pedestal 以及ISP概述
网上的直接参考资料 1. https://zhuanlan.zhihu.com/p/36896537 2. https://blog.csdn.net/m0_38049850/article/deta ...
- 完全小白入门:python的下载和安装
1. 打开官网www.python.org,选择Downloads
- Mac安装mongodb并启动
1.选择自己版本下载,下载完成后进入Finder看下usr文件夹下是否有mongodb2.默认情况下是看不到需要我们输入指令:shift + command +G 输入 /usr/local 进入文件 ...
- 深入研究RocketMQ消费者是如何获取消息的
前言 小伙伴们,国庆都过的开心吗?国庆后的第一个工作日是不是很多小伙伴还沉浸在假期的心情中,没有工作状态呢? 那王子今天和大家聊一聊RocketMQ的消费者是如何获取消息的,通过学习知识来找回状态吧. ...
- 一文搞懂PV、UV、VV、IP及其关系与计算
写在前面 十一长假基本上过去了,很多小伙伴在假期当中还是保持着持续学习的心态,也有不少小伙伴在微信上问我,让我推送相关的文章.这个时候,我都是抽空来整理小伙伴们的问题,然后,按照顺序进行推文. PS: ...
- WesternCTF2018_shrine
这个想了半天没啥思路,直接查别人的wp,贴地址:https://blog.csdn.net/qq_42812036/article/details/104324923 0x00 开始的页面猛一看乱七八 ...
- 多测师讲解自动化测试 _RF关键字001_( 中)_高级讲师肖sir
1.关键字如下 1.1Get Text 1.2Get Value 2.#上下滑动(滚动条) Open Browser http://www.jd.com gc Maximize Browser Win ...
- day34 Pyhton 网络编程
一今日内容 # 函数 # 面向对象 # 进阶 # 网络编程 4 # 并发编程 6-7 # 概念 # 网络基础 # 局域网的概念 # 交换机和路由器的工作流程 # ip地址 # mac地址 # 子网掩码 ...
- 利用Image对象,建立Javascript前台错误日志记录
手记:摘自Javascript高级程序设计(第三版),利用Image对象发送请求,确实有很多优点,有时候这也许就是一个创意点,再次做个笔记供自己和大家参考. 原文: 开发 Web 应用程序过程中的一种 ...
- MySQL数据库基础-1
数据库原理 数据时代 信息创造价值 -结构化数据 关系完整,密切 -非结构化数据 数据散乱,相互关系不大 -半结构化数据 XML HTML 也不是完全没有结构,也不是特别规矩 MySQL适合管理结构化 ...