LeetCode-SearchinRotatedSortedArrayII

12/12/2015来源:Java教程人气:675

题目:

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

}