zoukankan      html  css  js  c++  java
  • LeetCode数组类的题目提交记录 【2】

    /***********************************************************************
    33. Search in Rotated Sorted Array

    Suppose an array sorted in ascending order 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.


    eg: 

    0 1 2 3 4//右边有序情况
    4 0 1 2 3//右边有序情况
    3 4 0 1 2//右边有序情况

    2 3 4 0 1//左边有序情况
    1 2 3 4 0//左边有序情况
    **************************************************************/

    //代码提交1
    //循环法--查找
    
    int search(int* nums, int numsSize, int target) {
        int Left = 0;
        int Right = numsSize - 1;
        int Middle;
        while (Left <= Right)
        {
            Middle = (Left + Right) / 2;
    
            if (*(nums + Left) == target)
                return Left;
            if (*(nums + Right) == target)
                return Right;
            if (*(nums + Middle) == target)
                return Middle;
    
            if (*(nums + Left) < *(nums + Middle))
            {
                if (*(nums + Left) <= target && target < *(nums + Middle))
                    Right = Middle-1;
                else
                    Left = Middle + 1;
                continue;
            }
            else
            {
                if (*(nums + Middle) < target && target <= *(nums + Right))
                    Left = Middle + 1;
                else
                    Right = Middle-1;
                continue;
            }
    
        }
        return -1;
    }

      

    //代码提交2
    //递归法---查找
    int searchFun(int* nums, int nLeft,int nRight,int ntargrt) {
        int Left = nLeft;
        int Right = nRight;
        int Middle = (Left + Right) / 2;
    
        if (Left <= Right)
        {
            if (*(nums + Middle) == ntargrt) {
                return Middle;
            }
    
            if (*(nums + Middle) < *(nums + Right))
            {
                if (*(nums + Middle) < ntargrt && ntargrt <= *(nums + Right)) 
                {
                    Left = Middle + 1;
                    return searchFun(nums,Left,Right,ntargrt);
                }
                else            
                {
                    Right = Middle - 1;
                    return searchFun(nums, Left, Right, ntargrt);
                }
            }
    
            if (*(nums + Middle) > *(nums + Right)) 
            {
                if (*(nums + Middle) > ntargrt && ntargrt > *(nums + Right))
                {
                    Right = Middle - 1;
                    return searchFun(nums, Left, Right, ntargrt);
                }
                else 
                {
                    Left = Middle + 1;
                    return searchFun(nums, Left, Right, ntargrt);
                }
            }
    
            return -1;
        }
    
        return -1;
    }
    int search(int* nums, int numsSize, int target) {
        int Left = 0;
        int Right = numsSize - 1;
    
        int resultNum = searchFun(nums,Left,Right,target);
        return resultNum;
    }

    /***********************************************************************
    81. 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?

    Suppose an array sorted in ascending order 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).

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

    The array may contain duplicates.

    **************************************************************/

    bool search(int* nums, int numsSize, int target) {
        int Left = 0;
        int Right = numsSize - 1;
        int Middle;
        while (Left <= Right)
        {
            Middle = (Left + Right) / 2;
    
            if (*(nums + Left) == target)
                return true;
            if (*(nums + Right) == target)
                return true;
            if (*(nums + Middle) == target)
                return true;
    
            if (*(nums + Left) < *(nums + Middle))
            {
                if (*(nums + Left) <= target && target < *(nums + Middle))
                    Right = Middle - 1;
                else
                    Left = Middle + 1;
                continue;
            }
            else if (*(nums + Left) > *(nums + Middle))
            {
                if (*(nums + Middle) < target && target <= *(nums + Right))
                    Left = Middle + 1;
                else
                    Right = Middle - 1;
                continue;
            }
            else {//*(nums + Left) == *(nums + Middle)则跳过当前位置上的重复
                Left++;
            }
    
        }
        return false;
    }

      

  • 相关阅读:
    jQuery 获取 select 值和文本
    js 获取字符串中最后一个斜杠后面的内容
    asp.net web.config 经典模式和集成模式相关配置
    IIS8 web.config 重定向之后 报错 500.19
    nodeJs学习-06 模块化、系统模块、自定义模块、express框架
    nodeJs学习-05 案例:http/fs/querystring/url
    nodeJs学习-04 POST数据请求,分段发送,分段接收
    nodeJs学习-03 GET数据请求,js拆解/querystring/url
    nodeJs学习-02 fs模块(文件操作)
    nodeJs学习-01 http模块
  • 原文地址:https://www.cnblogs.com/iamcdx-2017/p/8124372.html
Copyright © 2011-2022 走看看