leetcode Search in Rotated Sorted Array II题解

    xiaoxiao2022-06-27  135

    题目描述:

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

    (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

    You are given a target value to search. If found in the array return true, otherwise return false.

    Example 1:

    Input: nums = [2,5,6,0,0,1,2], target = 0 Output: true

    Example 2:

    Input: nums = [2,5,6,0,0,1,2], target = 3 Output: false

    中文理解:

    给定一个递增数组,按照其中某个元素旋转,得到两段递增有序拼接在一起的数组,判断某个给定值是不是在这个数组中。

    解题思路:

    直接遍历一遍,使用O(n)时间复杂度。

    代码(java):

    class Solution { public boolean search(int[] nums, int target) { for(int val:nums){ if(val==target)return true; } return false; } }

     


    最新回复(0)