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 ...
随机推荐
- 【学习笔记/题解】虚树/[SDOI2011]消耗战
题目戳我 \(\text{Solution:}\) 题目很显然可以设\(dp[i]\)表示\(i\)的子树内的关键点都不和\(i\)联通的最小待机,有如下\(dp\)方程: \(v\in son_u, ...
- minikube dashboard报503的错误
minikube start之后,minikube dashboard启动web界面报503错误 解决方案,删除掉c盘用户目录下的.kube和.minikube目录,重新启动,具体什么原因导致的呢,也 ...
- Docker笔记2:Docker 镜像仓库
Docker 镜像的官方仓库位于国外服务器上,在国内下载时比较慢,但是可以使用国内镜像市场的加速器(比如阿里云加速器)以提高拉取速度. Docker 官方的镜像市场,可以和 Gitlab 或 GitH ...
- fastadmin toggle switch 开关 ids 值为空的解决办法
这个是低版本的一个bug 官方已给出说明,由于项目原因未选择升级版本 现在讲解决办法 1.在自定义开关的字段假如table:table 2.修改require-table.js 在536行左右的 to ...
- lua 源码阅读 5.3.5 笔记
记录下吧,断断续续读了几周,收益还是很多的. 推荐阅读顺序: 1) 基础数据类型 lstring.c ltable.c lobject.c lfunc.c lstate.c 2) 标准库(这个相对简 ...
- Apache Jmeter 性能测试
今天在写性能测试报告的时候需要使用到数据,打算用做一下性能测试,然后在百度后发现了一款Apache开源的Jmeter压测工具 Jmeter概述: Apache JMeter是一款纯java编写负载功能 ...
- JS中实现Trim(),TrimStart(),TrimEnd() 的方法
//去除字符串头尾空格或指定字符 String.prototype.Trim = function (c) { if (c == null || c == "") { var st ...
- pytest框架: fixture之conftest.py
原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown ...
- PS矢量工具
4.1PS矢量及位图 (1)位图就是像素图,由一个个像素点组成:矢量图是记录点到点的连线或者说程序算出来的图. (2)位图放大很多倍之后就会失真,可以看到像素点,类似于马赛克,所以有分辨率这一说. ( ...
- linux 常用命令及零散知识
磁盘管理常用命令 fdisk -l //展示磁盘使用情况 df -h //展示目录挂载情况 du -sh //查文件.文件夹的总大小 scp /home/a.txt /home/b.tx ...