leetcode 98: Search in Rotated Sorted Array II

Search in Rotated Sorted Array IIApr 20 '12

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.

the time complexity is O(n). this is a example u can not do it in log(n); 11111111111111111113111, target 3. without scan all elements in the array, there is no chance you can find the 3.

 

public class Solution {
    public boolean search(int[] A, int target) {
        // Start typing your Java solution below
        // DO NOT write main() function
        // 8,9,10, 1,2,3,4,5,6,7   target 9
        for(int i=0;i<A.length;i++) {
            if(A[i]==target) return true;
        }
        return false;
    }
}


 

posted @ 2013-03-04 11:45  西施豆腐渣  阅读(138)  评论(0编辑  收藏  举报