leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)
题目:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 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)已排序数组查找采用二分查找
2)关键找到临界点
实现:
一、我的代码:
1 class Solution { 2 public: 3 int search(int A[], int n, int target) { 4 if(n==0||n==1&&A[0]!=target) return -1; 5 if(A[0]==target) return 0; 6 int i=1; 7 while(A[i-1]<A[i]) i++; 8 9 int pre=binary_search(A,0,i,target); 10 int pos=binary_search(A,i,n-i,target); 11 return pre==-1?pos:pre; 12 } 13 private: 14 int binary_search(int *B,int lo,int len,int goal) 15 { 16 int low=lo; 17 int high=lo+len-1; 18 while(low<=high) 19 { 20 int middle=(low+high)/2; 21 if(goal==B[middle])//找到,返回index 22 return middle; 23 else if(B[middle]<goal)//在右边 24 low=middle+1; 25 else//在左边 26 high=middle-1; 27 } 28 return -1;//没有,返回-1 29 } 30 };
二、网上开源代码:
1 class Solution { 2 public: 3 int search(int A[], int n, int target) { 4 int first = 0, last = n-1; 5 while (first <= last) 6 { 7 const int mid = (first + last) / 2; 8 if (A[mid] == target) 9 return mid; 10 if (A[first] <= A[mid]) 11 { 12 if (A[first] <= target && target < A[mid]) 13 last = mid-1; 14 else 15 first = mid + 1; 16 } 17 else 18 { 19 if (A[mid] < target && target <= A[last]) 20 first = mid + 1; 21 else 22 last = mid-1; 23 } 24 } 25 return -1; 26 } 27 };
转载于:https://www.cnblogs.com/zhoutaotao/p/3822414.html
leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)相关推荐
- Search in Rotated Sorted Array - 循环有序数组查找问题
两道题 33. Search in Rotated Sorted Array https://leetcode.com/problems/search-in-rotated-sorted-array/ ...
- Rotated Sorted Array旋转排序数组相关题
1.什么是Rotated Sorted Array 在leetcode相关题目中,对Rotated Sorted Array相关的定义为: 整数数组 nums 按升序排列,数组中的值互不相同 . 在传 ...
- LeetCode 33. Search in Rotated Sorted Array
问题链接 LeetCode 33. Search in Rotated Sorted Array 题目解析 给定一个 "升序" 的 无重复 数组,从中寻找目标值."升序& ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- leetcode 81 Search in Rotated Sorted Array II ----- java
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [Leetcode] 33. Search in Rotated Sorted Array 解题报告
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- [LeetCode] 81. Search in Rotated Sorted Array II
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 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
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- python旋转排序数组_LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现...
题目描述: python实现Search in Rotated Sorted Array 搜索旋转排序数组 中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2, ...
最新文章
- 【ASP.NET】ASP.NET中权限验证使用OnAuthorization实现
- java如何给一个链表定义和传值_如何在CUDA中为Transformer编写一个PyTorch自定义层...
- 如何获取MDC产品文档
- Python3将xml文件解析为Python对象
- 云栖大会100位顶级大咖演讲PPT+视频全分享!
- 计算机中级职称报考入口,2020年9月计算机职称考试报名流程(附入口)
- 学习《深度学习入门:基于Python的理论与实现》高清中文版PDF+源代码
- 【渝粤教育】国家开放大学2018年秋季 0363-21T市场调查与预测 参考试题
- 台式机XP系统调节屏幕亮度
- SVN忽略提交文件设置
- HTML5小游戏--数字炸弹
- SSL P2712 跳格子
- chrome浏览器 Adobe Flash 版本太旧 无法播放视频解决办法
- SLAM传感器篇:Velodyne激光雷达
- 检测mysql字符正确_mysql字符集的测试
- linux5关闭apic服务,Linux中断 - APIC
- maven项目安装本地包实战演示
- 国家职业资格目录(执业资格,从业资格,职称)与各类国家证书
- 托管网站需要知道的网站优化指标有哪些
- 一、CameraService启动流程