LeetCode OJ--Search in Rotated Sorted Array
http://oj.leetcode.com/problems/search-in-rotated-sorted-array/
转换了一次的有序数组,进行类似二分查找。
从begin到mid和从mid到end,二者中肯定有一个是有序的。
#include <iostream>
using namespace std; class Solution {
public:
int binarySearch(int A[],int target,int begin,int end)
{
int mid = (begin+end)/; if(target == A[mid])
return mid;
if(target == A[begin])
return begin;
if(target == A[end])
return end;
if(begin>=end)
return -; if(A[begin]<A[mid])//前面有序
{
if(target>A[begin] && target<A[mid]) //在前面
return binarySearch(A,target,begin,mid-);
else
return binarySearch(A,target,mid+,end); //在后面
}
else if(A[mid]<A[end])//后面有序
{
if(target>A[mid] && target<A[end]) //在后面
return binarySearch(A,target,mid+,end);
else
return binarySearch(A,target,begin,mid-);//在前面
}
return -;
}
int search(int A[], int n, int target) {
return binarySearch(A,target,,n-);
}
}; int main()
{
Solution myS;
int A[] = {,,,,,,};
int ans = myS.search(A,,);
cout<<ans<<endl;
return ; }
LeetCode OJ--Search in Rotated Sorted Array的更多相关文章
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- Java for LeetCode 081 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 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 ...
- Leetcode系列-Search in Rotated Sorted Array
做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- OpenCascade:屏闪问题。
1.在OnDraw中同时调用用V3d_View::Redaw()和 V3d_View::FitAll();可暂时解决. 2.在OnDraw中同时调用用V3d_View::Update();
- navicat 常用快捷键
1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句3.ctrl+shift +/ 解除注释4.ctrl+r 运行查询窗口的 ...
- Java简答题附答案
1. Java有没有goto? 有, Goto语句在java中作为保留字, 并没有实现它. 带标号的break, continue局限于循环体中跳转 带标号的goto可以在一个函数(c语言)中任意跳转 ...
- JavaScript中数据类型和typeof返回的数据类型
除了上图,要注意三点:1.symbol是ES6中新增的数据类型 2.typeof(null)结果是Object 3.typeof(Object)和typeof(Array)的结果是function,因 ...
- plsql循环的简单实例
declare v_id tbl_regions.regions_id%type; begin .. loop select t.regions_id into v_id from tbl_regio ...
- js截屏
<html><head> <meta name="layout" content="main"> <meta http ...
- 验证debug思路之从寄存器开始
对于boot a peripherial or module 一般都是配置一系列的寄存器(有可能有时间等方便的写入读出要求). 1.确保寄存器的读写按spec要求完成.<====可以通过波形查看 ...
- [php] 表单注意事项
什么是 htmlspecialchars()方法? htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 预定义的字符是: & (和号) 成为 & &q ...
- Python 列表相关
python列表 列表推导式 例1 [ i*i for i in range(10) ] 打印如下: >>> [i*i for i in range(10)] [0, 1, 4, 9 ...
- Day08字符编码
Day08: 知识储备: 硬盘:由硬盘加载到内存,cpu从内存中取 软件产生的数据都是先保存在内存中 文件,输入文字,保存到内存,内存是硬件,硬件只能保存2进制,所以需要转换 文本编辑器,输入文字的时 ...