In less-than-linear time, find the duplicate in a sorted array

前端 未结 7 1628
名媛妹妹
名媛妹妹 2020-12-31 11:00

Today, an interviewer asked me this question. My immediate response was that we could simply do a linear search, comparing the current element with the previous element in t

7条回答
  •  天命终不由人
    2020-12-31 11:17

    How about that? (recursion style)

    public static int DuplicateBinaryFind(int[] arr, int left, int right)
    {
       int dup =0;
    
       if(left==right)
       {
          dup = left;
       }
       else
       {
            int middle = (left+right)\2;
            if(arr[middle]

提交回复
热议问题