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 ...
随机推荐
- javascript单元测试框架mochajs详解(转载)
章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建议使用箭头函数 钩子函数 钩 ...
- gdb插件使用方法
0x00 peda peda 安装: git clone https://github.com/longld/peda.git ~/peda echo "source ~/peda/peda ...
- POI导出excel项目(webwork)实例
后台action: public String exportExcel(){ this.setUserList(this.getUserService().findUserInfosByGroupID ...
- UINavgationController
UINavigationBar和UINavigationItem是iOS开发中常用的控件. 1.设置导航栏标题 self.title = @"iOS开发:iOSDevTip"; ...
- atlas 日志分析脚本
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: log_analyze.py #@time: 2018/8/23 17: ...
- rz
Linux系统简单易用的上传下载命令rz和sz sudo yum install lrzsz -y 上传:rz 下载:sz
- js常见面试题
1.大小写转化,将字符串转化成驼峰的方法 例:border-bottom-color转化为:borderBottomColor var str="border-bottom-color&qu ...
- Python中单元测试出错了,会怎么样?
在上一篇中,我们看到了单元测试正常通过时的情况,如果我们边修改一下要测试的函数,然后再运行单元测试,会出现什么情况. 原say_hello_name.py def hello_name(name): ...
- Django2.0+小程序技术打造微信小程序助手百度云
加Q[965546358]获取资源 第1章 课程导学 第2章 小程序开发入门 从几个方面介绍小程序开发相关的内容,包括小程序开发者账号注册.小程序开发流程.小程序开发规范.小程序常用的API,例如网络 ...
- Python之路-基础数据类型之列表 元组
列表的定义 列表是Python基础数据类型之一,它是以[ ]括起来, 每个元素用' , '隔开而且可以存放各种数据类型: lst = [1,2,'你好','num'] 列表的索引和切片 与字符串类似, ...