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

  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 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

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

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

随机推荐

  1. LCD framebuffer驱动设计文档

    内容提要:1. android display相关的名词2. 调试LCD驱动需要注意的步骤3. 关于帧缓冲区及I/O内存---------------------------------------- ...

  2. Maven : 将Jar安装到本地仓库和Jar上传到私服 转

    http://blog.csdn.net/we_shell/article/details/49819221 Jar的maven配置 <dependency><groupId> ...

  3. equals()和hashCode()区别?

    equals()和hashCode()区别? ------------------------------------------------- equals():反映的是对象或变量具体的值,即两个对 ...

  4. PAT 1078. Hashing

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  5. JavaScript实现遮罩层

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 今日又遇无法启动apache

    前几天安装了.NET要用到的MSSERVER,之后apache就running none of service. 把微软的MSSERVER停用掉服务就没问题.哪部分冲突了

  7. 基于前端javascript的搜索功能

    发表于 2013/11/07 当在数据量不是很大,而且没有后端对应的功能接口的时候,一些简单的搜索功能基本上是前端去实现的,正好最近用到,写了一个,贴出来和大家分享: 功能描述: 按下键盘后及时搜索条 ...

  8. [Javascript] Linting JavaScript with ESLint

    ESLint is a JavaScript linter (static analysis tool) that offers full support for ES6, JSX, and othe ...

  9. CPP: 跨平台生成GUID/UUID

    #ifndef XGUID_H#define XGUID_H #include <string>#include <stdio.h>#ifdef WIN32#include & ...

  10. Linux学习笔记总结--ssh认证登录

    原理简介 SSH证书认证登录的基础是一对唯一匹配密钥: 私钥(private key)和公钥(public key).公钥用于对数据进行加密,而且只能用于加密.而私钥只能对使用所匹配的公钥,所加密过的 ...