(二分查找 结构体) leetcode33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order 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.
Your algorithm's runtime complexity must be in the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
--------------------------------------------------------------------------------
这个就是查找一个数的索引。查找的方法不限。我此处用的是二分查找。就是先用结构体保存数和它的索引,然后对于结构体进行排序,最后用二分查找。emmm,有点麻烦
C++代码:
struct points{
int num;
int index;
}p[];
inline bool cmp(points a,points b){
return a.num < b.num;
}
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size() == ) return -;
for(int i = ; i < nums.size(); i++){
p[i].num = nums[i];
p[i].index = i;
}
sort(p,p + nums.size(),cmp);
int ans = Binary(p,,nums.size() - ,target);
if(ans == -) return -;
else return p[ans].index;
}
int Binary(points nums[],int left,int right,int target){
while(left <= right){
int mid = left + (right - left)/;
if(nums[mid].num == target) return mid;
else if(nums[mid].num > target) right = mid - ;
else left = mid + ;
}
return -;
}
};
(二分查找 结构体) leetcode33. Search in Rotated Sorted Array的更多相关文章
- LeetCode33 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 ...
- Leetcode33.Search in Rotated Sorted Array搜索旋转排序数组
假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值, ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 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(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
随机推荐
- sed 查找文件的某一行内容
1,查找文件text中第三行的内容 命令: sed -n '3p' text 2,查找文件text中第二行到第四行的内容 命令: sed -n '2,4p' text
- LeetCode算法题-Diameter of Binary Tree(Java实现)
这是悦乐书的第257次更新,第270篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第124题(顺位题号是543).给定二叉树,您需要计算树的直径长度. 二叉树的直径是树中 ...
- Pycharm 消除波浪线
转载:https://blog.csdn.net/youyouran12314/article/details/77719261 Pycharm环境下显示 在代码中显示许多波浪线,查看详情显示Spel ...
- Python 函数初识 (1)
一.今日主要内容 认识函数 函数:对功能或者动作的封装(定义) 语法: def 函数名字(形参) 函数体 函数的调用格式:函数名(实参) 函数的返回值 关键字:return 终止函数的运行 1.函数内 ...
- linux 本机内核模块
make -C /lib/modules/4.13.0-36-generic/build M=/$(pwd) modules 内核里只需要写obj-m:=hello.o
- JS 禁止Ctrl+C + 禁止右键操作
<script type="text/javascript"> document.oncontextmenu = new Function("return f ...
- flask刷新token
我们在做前后端分离的项目中,最常用的都是使用token认证. 登录后将用户信息,过期时间以及私钥一起加密生成token,但是比较头疼的就是token过期刷新的问题,因为用户在登录后,如果在使用过程中, ...
- UEFI和Legacy及UEFI+Legacy启动的区别
uefi和legacy是两种不同的引导方式,uefi是新式的BIOS,legacy是传统BIOS.你在UEFI模式下安装的系统,只能用UEFI模式引导:同理,如果你是在Legacy模式下安装的系统,也 ...
- 监听 在xshell中
- 求100之内的素质并输出(最优算法)-PHP面试题
曾经第一次面试题中的题目, 今天碰巧看到整理一下 当时用了最基本的算法写出来了, 两个for循环, 一个一个取余, 是质数就放进结果数组中 代码如下, 检查代码运行时间的代码是来对比三种不同算法的优劣 ...