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.

思路:找最小值,然后两边分别二分查找。

最小值找法:1. 只要小于上一个值即可。第一个只要小于最后一个(但是线性方法较慢) 2. 同样利用二分查找。

int binarySearch(int A[], int l, int h, int target) {
while(l <= h) {
int mid = (l+h) >> 1;
if(A[mid] > target) h = mid-1;
else if(A[mid] < target) l = mid+1;
else return mid;
}
return -1;
}
class Solution {
public:
int search(int A[], int n, int target) {
int low = 0, high = n-1, mid = 0;
while(A[low] > A[high]) {
if(low + 1 == high) {
mid = high;
break;
}
mid = (low + high) >> 1;
if(A[mid] < A[high]) high = mid;
else if(A[mid] > A[low]) low = mid;
}
return max(binarySearch(A, 0, mid-1, target), binarySearch(A, mid, n-1, target));
}
};

Search in Rotated Sorted Array II

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

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

思路: 同上,除了找最小值时,需要在 A[l] = A[mid] = A[h] 这种情况时,顺序走一遍找中值。

int binarySearch(int A[], int l, int h, int target) {
while(l <= h) {
int mid = (l+h) >> 1;
if(A[mid] < target) l = mid+1;
else if(A[mid] > target) h = mid-1;
else return true;
}
return 0;
}
int getMid2(int A[], int l, int h, int target) {
int id = l;
while(++l < h) {
if(A[l] < A[l-1]) {
id = l;
}
}
return id;
}
int getMid(int A[], int n, int target) {
int l = 0, h = n-1, mid = 0;
while(A[l] >= A[h]) {
if(l + 1 == h) {
mid = h;
break;
}
mid = (l+h) >> 1;
if(A[mid] == A[h] && A[mid] == A[l]) return getMid2(A, l, h, target);
if(A[mid] <= A[h]) h = mid;
else if(A[mid] >= A[l]) l = mid;
}
return mid;
}
class Solution {
public:
bool search(int A[], int n, int target) {
int m = getMid(A, n, target);
return binarySearch(A, 0, m-1, target) || binarySearch(A, m, n-1, target); }
};

49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II的更多相关文章

  1. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  2. eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面

    eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...

  3. Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)

    原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

  4. Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)

    原文:Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...

  5. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

  6. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

  7. Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target

    问题:链接 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  8. sorted matrix - search & find-k-th

    sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...

  9. Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...

随机推荐

  1. POJ 2184 01背包+负数处理

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 D ...

  2. Sae上传war包找不到编译文件

    最近在试新浪的sae平台,有个问题困扰了几天,现在想来还是自己对JAVA的理解不深入,这里写下仅作记忆. 问题:在整微信公众号平台进开发者中心的时候,token验证一直是失败的.但是项目在本地的环境中 ...

  3. 2014年5月份第1周51Aspx源码发布详情

    郑州某高校学生考评系统源码  2014-5-5 [VS2008]功能介绍:   1.用户角色有部主任.教师.学生等.   2.可添加班级考评项目.学生考评项目.   3.可指定学生对班级.学生某考评项 ...

  4. javaweb--上传文件UploadServlet1.java

    package cn.itcast.web.servlet; import java.io.File;import java.io.FileOutputStream;import java.io.IO ...

  5. RedHad中yum安装与使用

    yum的安装对于linux来说,是一个福音,至少安装软件来说,非常非常方便,以前使用rpm安装,那个各种依赖,哎,说多了都是泪,现在有这个yum就方便多了. 此处记录redhad的安装.其实我也是借鉴 ...

  6. iOS-服务器文件断点下载

    文件下载基本步骤:1.获取下载链接,创建响应发送请求.(使用异步请求,避免因文件过大下载时间长而阻塞主线程).2.当接到响应时在下载目录中创建文件.创建文件使用NSFileHandle进行文件内部处理 ...

  7. NIC bonding

    Bonding is the same as port trunking. In the following I will use the word bonding because practical ...

  8. Java-->利用URL类下载图片

    --> 通过get 请求访问图片地址,将通过服务器响应的数据(即图片数据)存到本地文件中... --> HttpURLConnectionUtil 工具类 package com.drag ...

  9. LintCode Singleton

    Singleton 3 大要素: 1.有private static的句柄(成员变量即field) 2. constructor 必须为private 3.有public static的getInst ...

  10. ueditor在使用requirejs时,报ZeroClipboard undefined错误

    再网上找到了 http://blog.csdn.net/xundh/article/details/44536665       这样一篇文章, 其中原因说的很明白了 是因为在有requirejs时, ...