33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II && 153. Find Minimum in Rotated Sorted Array && 154. Find Minimum in Rotated Sorted Array II


33. Search in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Binary SearchArray Solution: Determine where the target is can cover the special case that leftMin < rightMax.
public class Solution {
  public int search(int[] nums, int target) {
    int len = nums.length;
    int leftMin = nums[0];
    int rightMax = nums[len - 1];

    if (target > rightMax && target < leftMin)
      return -1;
    boolean targetAtLeft = target >= leftMin;

    int left = 0;
    int right = len - 1;

    while (left <= right) {
      int mid = (left + right) / 2;
      int midVal = nums[mid];
      if (midVal == target)
        return mid;
      if (targetAtLeft) {
        if (midVal >= leftMin && midVal < target)
          left = mid + 1; // go right
        else
          right = mid - 1; //go left
      } else {
        if (midVal <= rightMax && midVal > target)
          right = mid - 1; // go left
        else
          left = mid + 1; //go right
      }
    }
    return -1;
  }
}

81. Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":
What ifduplicatesare allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

ArrayBinary Search
public class Solution {
    public boolean search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int rightMax = nums[right];
        if(nums[left] == target)
            return true;
        //Do this while loop to convert it back to problem 33.
        while(nums[left] == rightMax) {
            ++left;
            if(left>right)
                return false;
        }
        
        int leftMin = nums[left];
        if (target > rightMax && target < leftMin)
          return false;
        boolean targetAtLeft = target >= leftMin;
    
        while (left <= right) {
          int mid = (left + right) / 2;
          int midVal = nums[mid];
          if (midVal == target)
            return true;
          if (targetAtLeft) {
            if (midVal >= leftMin && midVal < target)
              left = mid + 1; // go right
            else
              right = mid - 1; //go left
          } else {
            if (midVal <= rightMax && midVal > target)
              right = mid - 1; // go left
            else
              left = mid + 1; //go right
          }
        }
        return false;
    }
}

153. Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.

ArrayBinary Search
public class Solution {
  public int findMin(int[] nums) {
    int left = 0;
    int right = nums.length - 1;

    while (left < right) {
      int mid = left + (right - left) / 2;
      if (nums[mid] > nums[right])
        left = mid + 1;
      else
        right = mid;
    }
    return nums[left];
  }
}

154. Find Minimum in Rotated Sorted Array II

Follow upfor "Find Minimum in Rotated Sorted Array":
What ifduplicatesare allowed?

Would this affect the run-time complexity? How and why?

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

ArrayBinary Search
public class Solution {
  public int findMin(int[] nums) {
    int left = 0;
    int right = nums.length - 1;

    while (left < right) {
      int mid = left + (right - left) / 2;
      if (nums[mid] > nums[right])
        left = mid + 1;
      else if (nums[mid] < nums[right])
        right = mid;
      else
        --right;
    }
    return nums[left];
  }
}

优质内容筛选与推荐>>
1、Python百度音乐小爬虫
2、opencv中RGB转HSV
3、MySQL 5.1参考手册
4、盒子模型布局
5、20165104孟凡斌-第一次Java考试课下作业


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn