题目描述

给出一个转动过的有序数组,你事先不知道该数组转动了多少
(例如,0 1 2 4 5 6 7可能变为4 5 6 7 0 1 2).
在数组中搜索给出的目标值,如果能在数组中找到,返回它的索引,否则返回-1。
假设数组中不存在重复项。

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 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

输入

复制

[1],0

输出

复制

-1

class Solution {
public:
    bool search(int A[], int n, int target) {
        for(int i = 0;i<n;i++)
            {
            if(A[i]==target)
                return true;
        }
        return false;
    }
};

class Solution {
public:
    bool search(int A[], int n, int target) {
      int low = 0, high = n - 1;
        while(low <= high){
            int mid = (low + high) / 2;
            if(A[mid] == target)
                return true;
            if(A[low] == A[mid] && A[mid] == A[high]){
                low++;
                high--;
            }else if(A[low] <= A[mid]){  //left sorted
                if(A[low] <= target && A[mid] > target){
                    high = mid - 1;
                }
                else
                    low = mid + 1;
            }else if(A[mid] <= A[high]){
                if(A[mid] < target && A[high] >= target){
                    low = mid + 1;
                }
                else
                    high = mid - 1;
            }
                  
        }
        return false;  
    }
};

#
#
# @param A int整型一维数组
# @param target int整型
# @return bool布尔型
#
class Solution:
    def search(self , A , target ):
        # write code here
        return target in A

leetcode117search-in-rotated-sorted-array的更多相关文章

  1. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

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

  2. [LeetCode] Find Minimum 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 migh ...

  3. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

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

  4. [LeetCode] 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 migh ...

  5. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

  6. LintCode Find Minimum In Rotated Sorted Array

    1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...

  7. LeetCode-Search in Rotated Sorted Array II

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

  8. Leetcode Find Minimum in Rotated Sorted Array I and II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  10. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

随机推荐

  1. ISP-OB, pedestal 以及ISP概述

    网上的直接参考资料 1. https://zhuanlan.zhihu.com/p/36896537 2. https://blog.csdn.net/m0_38049850/article/deta ...

  2. 【记】《.net之美》之读书笔记(二) C#中的泛型

    前言 上一篇读书笔记,很多小伙伴说这本书很不错,所以趁着国庆假期,继续我的读书之旅,来跟随书中作者一起温习并掌握第二章的内容吧. 一.理解泛型 1.为什么要使用泛型?-----通过使用泛型,可以极大地 ...

  3. 轻轻松松学CSS:Grid布局

    网页布局总的来说经历了以下四个阶段: 1.古老的table表格布局,现在基本已被淘汰. 2.float浮动布局(或者position定位布局),借助float.position 等属性等进行布局,这种 ...

  4. CyclicBarrier原来是这样的

    上一篇聊了一下Semaphore信号灯的用法及源码,这一篇来聊一下CyclicBarrier的用法及解析. 官网解释: 允许一组线程全部等待彼此达到共同屏障点的同步辅助.循环阻塞在涉及固定大小的线程方 ...

  5. CV学习日志:CV开发常用库及其头文件

    CV开发过程中,通常会涉及以下库:(1)语言/视觉:C.CPP.QT.OpenCV(2)通信/模拟:ROS2.Gazebo.Webots(3)日志/数学:Eigen3.Gflags.Glog.Cere ...

  6. java基础语句翻译

    public static void main(String[] args) { System.out.println("人生中的第一个代码-----"); } } package ...

  7. MeteoInfoLab脚本示例:读取远程文件

    利用Unidata netCDF Java库对远程文件的读取能力(OpenDAP, ADDE, THREDDS等),可以读取远程文件并绘图.脚本程序: fn = 'http://monsoondata ...

  8. pytest使用小结

    一.pytest简洁和好处 自动发现测试用例 testloader 断言方便 ,自定义错误提示 assert 正则匹配 灵活运行指定的测试用例,指定模块,制定测试类,测试用例 -k 标签化,回归 正向 ...

  9. python面试题-python相关

    1. __new__.__init__区别,如何实现单例模式,有什么优点 __new__是一个静态方法,__init__是一个实例方法 __new__返回一个创建的实例,__init__什么都不返回 ...

  10. lumen-ioc容器测试 (4)

    lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...