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

      

  • 相关阅读:
    程序向informix数据库插入text类型的中文乱码问题
    深入理解Java:注解(Annotation)基本概念
    ssm架构的理解
    队列的java实现
    栈的java实现
    LinkedList的实现原理
    ArrayList的实现
    快速排序
    数据结构之桶排序
    leetcode
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11344630.html
Copyright © 2011-2022 走看看