LeetCode数组类的题目提交记录

Posted iamcdx-2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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., 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 (target == *(nums + Middle))
        {
            return Middle;
        }

        if (*(nums + Middle) < *(nums + Right))
        {//右边有序情况

            //1.把目标在限制死在中间值的  右边
            if (*(nums + Middle) < target &&  *(nums + Right) >= target)
                Left = Middle + 1;

            //2.其他情况则目标在中间值的 左边
            else 
                Right = Middle - 1;

            continue;
        }

        if (*(nums + Middle) > *(nums + Right))
        {//左边有序情况

            //1.把目标限制死在中间值的 左边
            if (*(nums + Middle) > target &&  *(nums + Right) < target)
                Right = Middle - 1;
                
            //2.其他情况则目标在中间值的 右边
            else
                Left = Middle + 1;

            continue;
        }

        return -1;

    }
    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;
}

 

  

 









以上是关于LeetCode数组类的题目提交记录 的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode题目记录-643. 子数组最大平均数 I(C++代码实现)

Leetcode.1024 视频拼接

LeetCode题目记录-646. 最长数对链(C++代码实现)

817. Linked List Components - LeetCode

LeetCode810. 黑板异或游戏/455. 分发饼干/剑指Offer 53 - I. 在排序数组中查找数字 I/53 - II. 0~n-1中缺失的数字/54. 二叉搜索树的第k大节点(代码片段

746. 使用最小花费爬楼梯『简单』