leetcode117:search-rotated-sorted-array
2024-10-18 16:28:14
题目描述
给出一个转动过的有序数组,你事先不知道该数组转动了多少
(例如,0 1 2 4 5 6 7可能变为4 5 6 7 0 1 2).
在数组中搜索给出的目标值,如果能在数组中找到,返回它的索引,否则返回-1。
假设数组中不存在重复项。
(例如,0 1 2 4 5 6 7可能变为4 5 6 7 0 1 2).
在数组中搜索给出的目标值,如果能在数组中找到,返回它的索引,否则返回-1。
假设数组中不存在重复项。
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
示例1
输出
-1
class
Solution {
public
:
int
search(
int
A[],
int
n,
int
target) {
if
(A == nullptr||n==0)
return
-1;
int
left = 0;
int
right = n-1;
while
(left<=right){
int
mid = left + (right - left)/2;
if
(A[mid] == target)
return
mid;
if
(A[left] <= A[mid]){
if
(A[left]<=target&&A[mid]>target)
right = mid-1;
else
left = mid+1;
}
else
{
if
(A[mid]<target&&A[right]>=target)
left = mid+1;
else
right = mid-1;
}
}
return
-1;
}
};
最新文章
- Redis集群搭建1
- 【原】Python 用例:二进制写入和读取文件内容
- JVM之类加载器中篇
- Json.net 忽略实体某些属性的序列化
- tomcat文件服务配置
- 调试CRM JS开发
- HTML5 game engines
- CentOS 6.4 中安装部署 Nutch 1.7
- Samza在YARN上的启动过程 =》 之二 submitApplication
- Memcached源码分析——hash
- day04
- Flex中如何通过设置GridLines对象的horizontalAlternateFill样式交错显示LineSeries图表背景颜色的例子
- PHP初入,基础知识点整理(样式表&;选择器的使用整理)
- FaaS技术框架
- openlayers3 基础(常见方法,类及实现)
- cnpm install -g live-server 安装服务
- Leetcode:204
- npm 全局执行 update 、 outdated 出现 npm-debug.log 404 错误的问题
- UITableViewCell 获取当前位置
- Liunx软Raid实现
热门文章
- 【题解】[CQOI]动态逆序对
- 手写一个类SpringBoot的HTTP框架:几十行代码基于Netty搭建一个 HTTP Server
- fastjson配置序列化过滤转换
- 【linux】基础命令一
- ztree通过ajax加载json数据中文乱码的解决方法:springmvc配置
- python与嵌入式的火花
- .net 手动建DataTable 获取DataTable列名 修改DataTable 列的顺序
- 如何使用 Gin 和 Gorm 搭建一个简单的 API 服务 (二)
- 【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
- JS图片的放大与缩小