Leetcode系列-Search in Rotated Sorted Array
做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的。希望自己能继续坚持下去,在校招前能解决超过一百题吧。
其实这些题就是用来训练你的解题思路的,做多了的话,才能在校招时面对编程题目,能立即有一个解题的流程,知道这是哪一种类型的题目,一般用什么样的套路,思路会很清晰,比如基础的字符串,数组操作会用到一些排序算法,还有一类是算法思想的题目,比如DP(动态规划),分治,回溯法,贪心等等,多练,多思考,多总结。
‘庖丁解牛,恢恢乎游刃有余’,无他,唯手熟尔!希望自己通过训练,也能在算法这一块做到手熟。。。
言归正题。
今天正好做到这道题,仔细一看竟然是今年参加的去哪儿网实习生招聘的第一道算法题,因此把他拿来记录一下。
具体信息如下:
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.
我的方案如下:
class Solution {
public:
int binarySearch(int *arr,int beg,int end,int target){
if (beg <= end){
int mid = (beg+end)/;
if (arr[mid] < target)
return binarySearch(arr,mid+,end,target);
if (arr[mid] > target)
return binarySearch(arr,beg,mid-,target);
if (arr[mid] == target)
return mid;
}
return -;
}
int search(int A[], int n, int target) {
if (n==)
return -;
if (n==)
if (A[] != target)
return -;
else
return ;
int down = ;
for (int i=;i<n;i++)
if (A[i]<A[i-])
down = i;
int n1 = binarySearch(A,,down-,target);
int n2 = binarySearch(A,down,n-,target);
if (n1 == -)
if (n2 == -)
return -;
else
return n2;
else
return n1;
}
};
Ac过。
Leetcode系列-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 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
随机推荐
- Cogs 309. [USACO 3.2] 香甜的黄油 dijkstra,堆,最短路,floyd
题目:http://cojs.tk/cogs/problem/problem.php?pid=309 309. [USACO 3.2] 香甜的黄油 ★★ 输入文件:butter.in 输出文件 ...
- 每隔一段时间执行一次函数。window.setTimeout
timer2 = window.setTimeout("showTaxi()", 30000);//30秒从后台获取一次数据,显示在地图上. 原来试过 setInterval . ...
- 使用DNSAgent拦截特定域名
开发程序时,为方便测试,需要把本来发往abc.com的数据发到本地. 最简单的方法是直接在程序中修改,把abc.com修改为需要的地址. 但这样提交代码时,容易把调试地址给提交到服务器. 或是嵌入式设 ...
- install Active Directory域控制器
设置Active Directory域控制器 正如我们在网络与系统配置专题文章中所提到的那样,我们已将两部服务器设置为对应于内部域“intdomain.com”的Active Directory域控制 ...
- mysql left( right ) join 使用on 与where的差异
之前一直很困扰为什么left join要么一查没有数据,要么出现很多条重复数据. 百思不得其解,后来在网上到处找资料,总算明白了. 一定要理解下面几点: (1)主表条件在on后面时附表只取满足主表筛 ...
- DiscreteSeekBar---->SeekBar的使用
build: compile 'org.adw.library:discrete-seekbar:1.0.0' 在布局中的使用: <org.adw.library.widgets.discret ...
- nginx定时备份access访问日志并重启nginx
用.sh脚本写了备份日志脚本 其实就是转移文件改名后重新建一个空文件 mv /alidata/log/nginx/access/wxtest.log /alidata/log/nginx/access ...
- 【设计模式 - 22】之策略模式(Strategy)
1 模式简介 在策略模式中,一个类的行为或其算法可以在运行时改变.策略模式定义了一系列算法,把它们一个个封装起来,并且使它们可以互相替换. 策略模式的优点: 1) 算法可以自由 ...
- GXT之旅:第三章:表单和窗口(4)——表单的提交和RPC
表单使用HTTP提交 表单有两种提交方式,第一种就是传统的HTTP提交. 最直接的步骤就是: 使用FormPanel的setAction()方法,去定义submit的URL 使用FormPanel的i ...
- PowerShell 导出SharePoint管理中心解决方式
PowerShell 导出SharePoint管理中心解决方式 SharePoint QQ群有人问能不能下载(导出)管理中心里的解决方式.由于在管理中心中点击解决方式会进入还有一个页面 ...