[LeetCode] Search in Rotated Sorted Array

二分 : 判断条件 当a[left] <= a[mid]时,可以肯定a[left..mid]是升序的

循环有序 一共有以下两种情况

  第一种

    /

  /

 /

/   

     / 

    / 

条件: (A[mid] >= A[low]) ,low~mid 二分,mid~high 递归

第二种

  / 

 /

        /

      /  

     /

    / 

条件: (A[mid] < A[low]) ,low~mid 二分,mid~high 递归

 1 class Solution {
 2 public:
 3     int binarySearch(int A[], int low, int high, int target)
 4     {
 5         int mid = (high+low)/2;
 6         
 7         if(low <= high)
 8         {
 9             if(target == A[mid]) 
10                 return mid;
11             else if(target > A[mid])
12                 return binarySearch(A, mid+1, high, target);
13             else
14                 return binarySearch(A, low, mid-1, target);
15         }
16         return -1;
17     }
18     
19     int search(int A[], int n, int target) 
20     {
21         return search(A, 0, n-1, target);
22     }
23     
24     int search(int A[], int low, int high, int target) 
25     {
26         
27         int mid =  (high+low)/2;
28         
29         if(low > high)
30             return -1;
31         
32         if(A[mid] == target)
33             return mid;
34         
35         if(A[mid] >= A[low]) // the pivot is in the bottom half
36         {
37             if(target >= A[low] && target < A[mid]) // target in the first half
38             {
39                 return binarySearch(A, low, mid-1, target);
40             }
41             else // target in the bottom half
42             {
43                 return search(A, mid+1, high, target);
44             }
45         }
46         else // the pivot is in the first half
47         {
48             if(target >= A[mid+1] && target <= A[high]) // target in the bottom half
49             {
50                 return binarySearch(A, mid+1, high, target);
51             }
52             else // target in the first half
53             {
54                 return search(A, low, mid-1, target);
55             }            
56         }
57     }
58 };

 

迭代:

判读条件和上面一样

 

 1 class Solution {
 2 public:
 3     bool search(int A[], int n, int target) {
 4         int low = 0;
 5         int high = n-1;
 6         int mid ;
 7 
 8         
 9         while(low<=high)
10         {
11             mid= (low + high)/2;
12             
13             if(A[mid]==target)
14             {
15                 return true;
16             }
17                     
18             if(A[low]<=A[mid]) // the pivot is in the bottom half
19             {
20                 if(A[low]<=target && target < A[mid]) //target in the first half
21                     high = mid-1;
22                 else
23                     low = mid+1;//target in the bottom half
24             }
25             else // the pivot is in the first half
26             {
27                 if(A[mid] < target && target <= A[high])// the pivot is in the first half
28                     low = mid+1;
29                 else //target in the first half
30                     high = mid-1;
31             }
32             
33         }
34         return false;
35         
36     }
37 };

 

转载于:https://www.cnblogs.com/diegodu/p/3788663.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407671.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

失败者的特征

1 狂妄自大型:自以为老子天下第一,认为自己的认识才是正确的,才是没有错误的,实际上这些人的知识大部分来自于道听途说或是妄自没有求证的猜测就下定论,并一棍子打死,还不允许其它人也相信.2 口是心非型:心里面还是相信有某种力量能够支配人生,甚至还会经常拿着彩票的轨迹图天天…

@keyframes中translate和scale混用问题

当你动画的这个节点用到translate定位居中时&#xff0c;再使用动画scale就会出现不居中的问题 这时需要把keyframes中translate写在scale的前面就解决了 keyframes bubble-in {0% {transform:translateX(-50%) scale(0);}100% {transform:translateX(-50%) scale(1);} }

导入 IMP

Oracle 的导入实用程序 (Import utility) 允许从数据库提取数据&#xff0c;并且将数据写入操作系统文件。 imp 使用的基本格式&#xff1a; imp[username[/password[service]]] &#xff0c;以下例举 imp 常用用法。 1. 获取帮助 imp helpy 2. 导入一个完整数据库 imp sy…

Vue基础之组件

什么是组件&#xff1f; 组件&#xff08;Component&#xff09;是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素&#xff0c;封装可重用的代码。在较高层面上&#xff0c;组件是自定义元素&#xff0c; Vue.js 的编译器为它添加特殊功能。在有些情况下&#xff0c;组件也…

sql 保留小数

select ROUND(12.555, 2) --12.560 select cast(12.5550 as decimal(10,2)) --12.56 转载于:https://www.cnblogs.com/kedarui/p/3791337.html

React开发(105):没有定义变量报错

const { pageIndex, pageSize, selectTabVal, sortModel } this.state;

常用的前端JavaScript方法封装(49种)

1、输入一个值&#xff0c;返回其数据类型 function type(para) { return Object.prototype.toString.call(para) } 2、数组去重 function unique1(arr) { return […new Set(arr)] } function unique2(arr) { var obj {}; return arr.filter(ele > { if (!obj[ele]) { ob…

发一个flash+PHP的简单上传代码

示例文件1。Flash8:uploader.as--------------------------------------------------------------------------------import flash.net.FileReference; //调用上传控件&#xff0c;这个是必须的&#xff0c;要传文件就要用这个控件var frT…

Oracle SQL Loader数据导入

1、SQL LOADER是ORACLE的数据加载工具&#xff0c;通常用来将操作系统文件迁移到ORACLE数据库中。SQL*LOADER是大型数据仓库选择使用的加载方法&#xff0c;因为它提供了最快速的途径&#xff08;DIRECT&#xff0c;PARALLEL&#xff09;。 它使用的命令为&#xff1a;在NT下&a…

反射获取类的几种方法

1 public class Demo {2 3 /**4 * 反射&#xff1a;加载类&#xff0c;获得类的字节码5 * param args6 * throws ClassNotFoundException 7 */8 public static void main(String[] args) throws ClassNotFoundException {9 10 //…

Webpack基础之四个核心介绍

入口(Entry)&#xff1a; webpack 将创建所有应用程序的依赖关系图表(dependency graph)。图表的起点被称之为入口起点(entry point)。入口起点告诉 webpack 从哪里开始&#xff0c;并遵循着依赖关系图表知道要打包什么。可以将您应用程序的入口起点认为是根上下文(contextual…

React开发(106):方法定义 不然弹出框报错

hideSureModal () > {this.setState({sortModel: false,});};

为什么防火墙透传不过去VLAN11?

今天遇到一个问题请大家帮分析一下!2950上有两个VLAN需要通过防火墙透传VLAN1和VLAN11现在的问题是:VLAN11不能通过防火墙透传!vlan1可以透传过去.转载于:https://blog.51cto.com/liuzhu/59913

有关Java 锁原理

锁 锁是用来锁东西的&#xff0c;让别人打不开也看不到&#xff01;在线程中&#xff0c;用这个“锁”隐喻来说明一个线程在“操作”一个目标&#xff08;如一个变量&#xff09;的时候&#xff0c;如果变量是被锁住的&#xff0c;那么其他线程就对这个目标既“操作”不了&…

不能以根用户身份运行 Google Chrome 浏览器

在fedora12中安装了chrome浏览器&#xff0c;但是一运行出现以下提示&#xff1a; 不能以根用户身份运行 Google Chrome 浏览器。请以普通用户身份启动“Google Chrome 浏览器”。要以根用户身份运行&#xff0c;您必须为个人资料信息的存储指定其他的“--user-data-dir”。 …

Taro+react开发(24)--this.state和this.props

由于 this.props 和 this.state 都用于描述组件的特性&#xff0c; 可能会产生混淆。一个简单的区分方法是&#xff0c;this.props表示那些一旦定义&#xff0c;就不再改变的特性&#xff0c;而 this.state 是会随着用户互动而产生变化的特性。

Webpack基础之入口起点

入口起点(Entry Points) 单个入口&#xff08;简写&#xff09;语法&#xff1a; 用法&#xff1a;entry: string|Array<string> entry 属性的单个入口语法&#xff0c;是下面的简写 当你向 entry 传入一个数组时会发生什么&#xff1f;向 entry 属性传入「文件路…

Tailwind CSS 是一个工具集 CSS 框架

Tailwind CSS 是一个工具集 CSS 框架&#xff0c; 助你快速实现定制化的网站设计。 Tailwind CSS 是一个高度可定制的基础层 CSS 框架&#xff0c;它为您提供了构建定制化设计所需的所有构建块&#xff0c;而无需重新覆盖任何内建于框架中的设计风格。 官网&#xff1a;https:/…