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.
*
* “在旋转排序数组中搜索”的后续操作:
* 如果允许重复怎么办?
* 这会影响运行时的复杂性吗?如何以及为什么?
* 编写一个函数来确定给定目标是否在数组中。
*/

import java.util.Arrays;

/**
 * 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 Main47 {
    public static void main(String[] args) {
        int[] A = {0,2,3,4,6,7,1,2,3};
        System.out.println(search(A, 8));
        //Arrays.sort(A);
        //System.out.println(Arrays.binarySearch(A, 9)<0 ? false : true);
    }

    public static boolean search(int[] A, int target) {
        int len = A.length;
        for (int i=0;i<len;i++) {
            if (A[i] == target) {
                return true;
            }
        }

        return false;
    }
}

  

原文地址:https://www.cnblogs.com/strive-19970713/p/11344630.html