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的更多相关文章

  1. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  2. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  5. [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. 思路 ...

  6. 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 ...

  7. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  8. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  9. Leetcode系列-Search in Rotated Sorted Array

    做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...

  10. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

随机推荐

  1. graphviz layer 教程(非布局)

    官方 pdf 上讲解的很少,没有图片. http://www.graphviz.org/wiki/how-use-drawing-layers-overlays 这里有图片,但是又没有说如何生成. 直 ...

  2. Ukulele 调音

    正常的持琴姿势时,从上到下依次是:4,3,2,1弦,音从上往下是:G,C,E,A: 3弦 - C - Do - D - Re 2弦 - E - Mi - F - Fa 4弦 - G -So 1弦 - ...

  3. 【图论 搜索】bzoj1064: [Noi2008]假面舞会

    做到最后发现还是读题比赛:不过还是很好的图论题的 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选 ...

  4. CSS 隐藏 visibility 属性

    定义和用法 visibility 属性规定元素是否可见. 提示:即使不可见的元素也会占据页面上的空间.请使用 "display" 属性来创建不占据页面空间的不可见元素. 说明 这个 ...

  5. Django ORM字段和字段参数

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  6. Day11名称空间,作用域,闭包函数

    Day11 1.函数对象: ①可以被引用 ​ ②可以作为另一个函数的参数 ​ ③可以作为另一个函数的返回值0 ​ ④可以被存储到容器类型中 2.函数嵌套: ①嵌套调用:在一个函数中调用了另一个函数 ​ ...

  7. JavaScript正则表达式-非捕获性分组

    非捕获性分组定义子表达式可以作为整体被修饰但是子表达式匹配结果不会被存储. 非捕获性分组通过将子表达式放在"?:"符号后. str = "img1.jpg,img2.jp ...

  8. Django框架简介及模板Template,filter

    Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...

  9. spring-cloud-sleuth 学习资源

    https://www.baeldung.com/spring-cloud-sleuth-single-application https://howtodoinjava.com/spring-clo ...

  10. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...