当前位置: 首页 > news >正文

LeetCode【33】搜索旋转排序数组

题目:
在这里插入图片描述
思路:
https://www.cnblogs.com/CherryTab/p/12196580.html

代码:

class Solution {int [] nums;int target;public int find_rotate_index(int left, int right) {if (nums[left] < nums[right])return 0;while (left <= right) {int pivot = (left + right) / 2;if (nums[pivot] > nums[pivot + 1])     // 注意这里找临界点,是和下一个坐标比较;return pivot + 1;else {if (nums[pivot] < nums[left])right = pivot - 1;elseleft = pivot + 1;}}return 0;}public int search(int left, int right) {/*Binary search*/while (left <= right) {int pivot = (left + right) / 2;if (nums[pivot] == target)return pivot;else {if (target < nums[pivot])right = pivot - 1;elseleft = pivot + 1;}}return -1;}public int search(int[] nums, int target) {this.nums = nums;this.target = target;int n = nums.length;if (n == 0)return -1;if (n == 1)return this.nums[0] == target ? 0 : -1;int rotate_index = find_rotate_index(0, n - 1);// if target is the smallest elementif (nums[rotate_index] == target)return rotate_index;// if array is not rotated, search in the entire arrayif (rotate_index == 0)return search(0, n - 1);if (target < nums[0])// search in the right sidereturn search(rotate_index, n - 1);// search in the left sidereturn search(0, rotate_index);}
}
http://www.lryc.cn/news/230481.html

相关文章:

  • 若依系统富文本框上传图片报错!
  • Azure 机器学习:MLOps - 使用 Azure 机器学习进行模型管理、部署和监视
  • CSDN每日一题学习训练——Java版(分数到小数、罗马数字转整数、x 的平方根)
  • 【2021集创赛】 RISC-V杯三等奖:基于E203 处理器的SM4算法硬件加速
  • SUMO道路封闭车辆绕行仿真实验【TraCI】
  • IDEA 无法搜索或者下载插件
  • unity 使用Vuforia扫描实体物体交互
  • IDEA接口调试插件不好找?这款免费用!
  • OpenCV图像坐标系
  • 【Proteus仿真】【Arduino单片机】DHT11温湿度
  • Linux--makefile
  • Anaconda学习备忘
  • uniapp运行到安卓模拟器一直在“同步手机端程序文件完成“界面解决办法
  • leetcode:876. 链表的中间结点
  • 【m98】webrtc vs2017构建带符号的debug库
  • 【读点论文】结构化剪枝
  • JimuReport积木报表 v1.6.5 版本发布—免费报表工具
  • 【开发工具】gitee还不用会?我直接拿捏 >_>
  • 算法通关村——位运算之位移的妙用
  • 【开题报告】基于uni-app的高校新生报道APP的设计与实现
  • Elasticsearch docker-compose 使用 Logstash 从 JSON 文件中预加载数据
  • <文件操作及常用的API>
  • 深入探讨Linux中的文本文件查看命令
  • asp.net企业员工档案信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目
  • WPF中的xmlns 和xmlns:x有什么区别?
  • 为什么流量卡禁区多,而手机卡却可以用呢?
  • Linux 桌面应用
  • NLP领域的突破催生大模型范式的形成与发展
  • 大模型的全面回顾,看透大模型 | A Comprehensive Overview of Large Language Models
  • 【瑞禧分享】碳化硅纳米线 SiC纳米线 <100nm SiC晶须 SiC短纤维