当前位置:网站首页>LeetCode刷题日记: 33、搜索旋转排序数组
LeetCode刷题日记: 33、搜索旋转排序数组
2022-08-02 01:44:00 【淡墨@~无痕】
整数数组 nums 按升序排列,数组中的值 互不相同 。
在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。
给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 target ,则返回它的下标,否则返回 -1 。
你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。
示例 1:
输入:nums = [4,5,6,7,0,1,2], target = 0
输出:4
示例 2:
输入:nums = [4,5,6,7,0,1,2], target = 3
输出:-1
示例 3:
输入:nums = [1], target = 0
输出:-1
提示:
1 <= nums.length <= 5000
-104 <= nums[i] <= 104
nums 中的每个值都 独一无二
题目数据保证 nums 在预先未知的某个下标上进行了旋转
-104 <= target <= 104
题解:
public static int search(int[] nums, int target) {
int res = -1;
int start = 0, end = nums.length-1;
if(nums.length == 0){
return -1;
}
if(nums.length == 1){
return nums[0] == target ? 0 : -1;
}
while (start <= end) {
int mid = (start + end)/2;
if(nums[mid] == target){
res = mid;
break;
}
// 以中点为核心,添加多一个判断,
if(nums[mid] >= nums[start]){
if(nums[start] <= target && target < nums[mid] ){
end = mid-1;
}else{
start = mid + 1;
}
}else if(nums[mid] < nums[start]){
if(nums[mid] < target && target <= nums[nums.length-1]){
start = mid + 1;
}else{
end = mid - 1;
}
}
}
return res;
}
边栏推荐
- Use flex-wrap to wrap lines in flex layout
- 3. Bean scope and life cycle
- 软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3
- HSDC is related to Independent Spanning Tree
- When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
- 秒懂大模型 | 3步搞定AI写摘要
- Why is on-chain governance so important, and how will Polkadot Gov 2.0 lead the development of on-chain governance?
- 6-25漏洞利用-irc后门利用
- TKU记一次单点QPS优化(顺祝ITEYE终于回来了)
- 『网易实习』周记(三)
猜你喜欢

传统企业数字化转型需要经过几个阶段?

typescript33-typescript高级概述

浅谈国产ERP的“横纵竖”三向发展态势

27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法!

typeof in typescript32-ts

记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换

Pcie the inbound and outbound

Why is on-chain governance so important, and how will Polkadot Gov 2.0 lead the development of on-chain governance?

YGG Guild Development Plan Season 1 Summary

3个月测试员自述:4个影响我职业生涯的重要技能
随机推荐
TKU记一次单点QPS优化(顺祝ITEYE终于回来了)
go泛型使用方法
Navicat数据显示不完全的解决方法
Pytorch seq2seq model architecture to achieve English translation tasks
信息化和数字化的本质区别是什么?
Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program
canal实现mysql数据同步
浅谈国产ERP的“横纵竖”三向发展态势
pcie inbound和outbound关系
typescript33 - high-level overview of typescript
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
typescript35-class的构造函数
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
HSDC is related to Independent Spanning Tree
Shell入门终章
Image fusion based on weighted 】 and pyramid image fusion with matlab code
Constructor instance method of typescript36-class
Can't connect to MySQL server on 'localhost3306' (10061) Simple and clear solution
Huawei's 5-year female test engineer resigns: what a painful realization...
AOF重写