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. ...
随机推荐
- [zz] makefile中=和:=的区别
转载自:http://www.cnblogs.com/wanqieddy/archive/2011/09/21/2184257.html 在Makefile中我们经常看到 = := ?= +=这几个赋 ...
- Python运行机制
闲来无事,简单画了一下Python的运行机制,纯属娱乐:
- hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 从MSN上拔下来的全世界国家下拉框(附带SQL执行脚本)
<select> <option value="AL">阿尔巴尼亚</option> <option value="DZ&quo ...
- kafka broker 进入 conflicted ephemeral node 死循环
转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/5621303.html 最近发现kafka一台服务器producer客户端写入时一直报错,查看该br ...
- PTA 07-图5 Saving James Bond - Hard Version (30分)
07-图5 Saving James Bond - Hard Version (30分) This time let us consider the situation in the movie ...
- 通讯录改造——MVC设计模式
将之前用servlet写的程序转化为jsp+servlet的简单的MVC的三层结构.项目中程序的包如图 首先是实体对象: package com.contactSystem.entiey; publi ...
- DragQueryFile
附件:http://files.cnblogs.com/xe2011/CSharp_DragQueryFile.rar using System.Runtime.InteropServices; th ...
- 使用lua给wireshark编写uTP的Dissector
lonelycastle做uTP的实验,使用wireshark捕包,但是最初没有找到wireshark下的uTP的dissector,每次都需要比对文档,这样做实验理解报文含义,效率非常低.作为程 ...
- bootstrap02导航菜单
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...