爱程序网

LeetCode - 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?

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

 

思路:

直接循环一遍查找

package array;

public class SearchInRotatedSortedArrayII {

    public boolean search(int[] nums, int target) {
        int len;
        if (nums == null || (len = nums.length) == 0) return false;
        for (int i = 0; i < len; ++i) 
            if (nums[i]==target)
                return true;
        return false;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] nums = { 3, 1, 1 };
        SearchInRotatedSortedArrayII s = new SearchInRotatedSortedArrayII();
        System.out.println(s.search(nums, 3));
    }

}

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助