Leetcode no. 81

来源:互联网 发布:北京装修多少钱 知乎 编辑:程序博客网 时间:2024/05/21 03:16

81. 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.


public class Solution {    public boolean search(int[] nums, int target) {        if (nums.length==0) return false;        int start= 0, end= nums.length-1;        while (start < end){            int center= (start+end)/2;            if (nums[center] == target) return true;            if (nums[center]> nums[end]){                if (target < nums[center] && target>= nums[start])                    end= center;                else start= center+1;            }else if (nums[center]< nums[end]){                if (target > nums[center] && target<=nums[end])                    start= center+1;                else end= center;            } else end--;        }        return nums[end]== target? true : false;    }}


0 0
原创粉丝点击