zoukankan      html  css  js  c++  java
  • 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.

    原题链接:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/

    题目:继续之前的题,在旋转过的有序数组中找目标值。若同意有反复的值呢?

    	public boolean search(int[] A, int target) {
    		int low = 0, high = A.length - 1;
    		while (low <= high) {
    			int mid = (low + high) / 2;
    			if (target == A[mid])
    				return true;
    			if (A[mid] > A[low]) {
    				if (target >= A[low] && target <= A[mid])
    					high = mid - 1;
    				else
    					low = mid + 1;
    			} else if (A[mid] < A[low]) {
    				if (target >= A[mid] && target <= A[high])
    					low = mid + 1;
    				else
    					high = mid - 1;
    			} else
    				low += 1;
    		}
    		return false;
    	}



  • 相关阅读:
    数据链路层
    补码加减法
    matlab函数
    HDU2159_二维完全背包问题
    HDU2844买表——多重背包初探
    HDU1025贫富平衡
    最大m段子段和
    01背包浮点数情况
    第K大01背包
    HDU2955 01背包
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5087115.html
Copyright © 2011-2022 走看看