当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Search in Rotated Sorted Array II

发表于: 2015-07-03   作者:hcx2013   来源:转载   浏览:
摘要: 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 ta

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) {
        int start = 0;
        int end = nums.length-1;
        return search(nums, target, start, end);
    }

	private boolean search(int[] nums, int target, int start, int end) {
		if (start>end) {
			return false;
		}
		int mid = (start+end)/2;
		if (target == nums[mid]) {
			return true;
		}
		if (nums[mid] == nums[end]) {
		    return search(nums, target, start, mid-1) || search(nums, target, mid+1, end);
		} else if (nums[mid] > nums[end]) {
			if (target<nums[mid] && target>=nums[start]) {
				return search(nums, target, start, mid-1);
			} else {
				return search(nums, target, mid+1, end);
			}
		} else {
			if (target>nums[mid] && target<=nums[end]) {
				return search(nums, target, mid+1, end);
			} else {
				return search(nums, target, start, mid-1);
			}
		}
	}
}

 

Search in Rotated Sorted Array II

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Wou
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6
Question: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6
Search in Rotated Sorted Array Implementation public class Solution { public int search(int[]
【题目】 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0
Search in Rotated Sorted Array 题目: Suppose a sorted array is rotated at some pivot unknown
【题目】 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed
原题链接:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题的大意
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号