在有序旋转数组中找到一个数

这里写图片描述

//在有序旋转数组中找到一个数
public class FindNum{

     public static boolean isContains(int[]arr,int num)
     {
          int low=0;
          int high=arr.length-1;
          int mid=0;
          while(low<=high)
          {
              mid=(low+high)/2;
              if(arr[mid]==num)
              {
                  return true;
              }
              if(arr[low]==arr[mid]&&arr[mid]==arr[high])
              {
                    while(low!=mid&&arr[low]==arr[mid])
                    {
                         low++;
                    }
                    if(low==mid)
                    {
                          low=mid+1;
                          continue;
                    }

              }
              if(arr[low]!=arr[mid])
              {
                  if(arr[mid]>arr[low])
                  {
                       if(num>=arr[low]&&num<arr[mid])
                       {
                          high=mid-1;
                       }else{
                           low=mid+1;
                       }
                  }else{
                         if(num>arr[mid]&&num<=arr[high])
                         {
                             low=mid+1;
                         }else{
                             high=mid-1;
                         }
                  }
              }else{

                    if(arr[mid]<arr[high])
                    {
                          if(num>arr[mid]&&num<=arr[high])
                          {
                                low=mid+1;
                          }else{
                                 high=mid-1;
                          }
                    }else{

                           if(num>=arr[low]&&num<arr[mid])
                           {
                             high=mid-1;
                           }else{
                              low=mid+1;
                           }

                    }
              }
          }
          return false;
     }

     public static  void main(String[]args)
     {
        //System.out.println("Hello");
         int arr[]={4,5,6,7,1,2,3};
         int num=3;
         System.out.println(isContains(arr,num));
     }
}

这里写图片描述

阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页