LeetCode(33 & 153 & 154):搜索旋转排序数组 Search in Rotated Sorted Array(Java)

234 篇文章 1 订阅
177 篇文章 0 订阅
这篇博客介绍了如何解决LeetCode中的搜索旋转排序数组问题,包括寻找目标值和最小值。文章强调了二分查找算法在解决这类问题中的应用,并讨论了处理旋转数组中翻转边界的情况,以及在存在重复元素时的解决方案。
摘要由CSDN通过智能技术生成

2019.6.15 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新)

github:https://github.com/ChopinXBP/LeetCode-Babel

题目要求O(log n),很明显需要用二分查找来做。不过分类逻辑却需要思考一下。

每次对半截断后,数组可能有两种情况,一种是包含翻转分界的子数组(头元素大于尾元素),另一种是正常升序的数组。在翻转数组中,又分为target位于翻转边界前和位于翻转边界后两种情况。分类完成后,按照正常的二分算法处理即可。

在寻找最小值的升级版题目中,由于可能会出现重复元素,导致无法判断区间,例如[1,0,1,1,1]和[1,1,1,0,1],可以采用end–来规避问题。


传送门:搜索旋转排序数组

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,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.

Your algorithm’s runtime complexity must be in the order of O(log n).

假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。

你可以假设数组中不存在重复的元素。

你的算法时间复杂度必须是 O(log n) 级别。

示例 1:
输入: nums = [4,5,6,7,0,1,2], target = 0
输出: 4

示例 2:
输入: nums = [4,5,6,7,0,1,2], target = 3
输出: -1


/**
 * 
 * @author ChopinXBP
 * Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
 * (i.e., [0,1,2,4,5,6,7] might become [4,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.
 * Your algorithm's runtime complexity must be in the order of O(log n).
 * 假设按照升序排序的数组在预先未知的某个点上进行了旋转。
 * ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
 * 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。
 * 你可以假设数组中不存在重复的元素。
 * 你的算法时间复杂度必须是 O(log n) 级别。
 * 
 */

public class SearchinRotatedSortedArray {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
    public int search(int[] nums, int target) {
        if(nums == null || nums.length == 0)return -1;
        
        int begin = 0;
        int end = nums.length - 1;
        
        while(begin <= end) {
        	
        	int mid = (begin + end) >> 1;
        	if(nums[mid] == target) {
        		return mid;
        	}
        	//数组翻转
        	else if(nums[begin] > nums[end] && nums[begin] <= nums[mid]) {
        		if(nums[end] == target)
        			return end;
        		if(target < nums[end] || target > nums[mid]) {
        			begin = mid + 1;
        		}
        		else {
        			end = mid - 1;
        		}
        	}
        	else if(nums[begin] > nums[end] && nums[begin] > nums[mid]) {
        		if(nums[end] == target)
        			return end;
        		if(target < nums[end] && target > nums[mid]) {
        			begin = mid + 1;
        		}
        		else {
        			end = mid - 1;
        		}
        	}
        	//数组有序
        	else {
        		if(target > nums[mid]) {
        			begin = mid + 1;
        		}else {
        			end = mid - 1;
        		}
        	}
        }
        
        return -1;
    }
    

}




传送门:寻找旋转排序数组中的最小值

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

Find the minimum element.

You may assume no duplicate exists in the array.

假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

请找出其中最小的元素。

你可以假设数组中不存在重复元素。

示例 1:
输入: [3,4,5,1,2]
输出: 1

示例 2:
输入: [4,5,6,7,0,1,2]
输出: 0


/**
 *
 * Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
 * (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]).
 * Find the minimum element.
 * You may assume no duplicate exists in the array.
 * 假设按照升序排序的数组在预先未知的某个点上进行了旋转。
 * ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
 * 请找出其中最小的元素。
 * 你可以假设数组中不存在重复元素。
 *
 */

public class FindMinimumInRotatedSortedArray {
    public int findMin(int[] nums) {
        int begin = 0;
        int end = nums.length - 1;
        while(begin < end){
            int mid = begin + ((end - begin) >> 1);
            if(nums[mid] < nums[begin]){
                end = mid;
            }else if(nums[end] <= nums[begin]){
                begin = mid + 1;
            }else{
                end = mid;
            }
        }
        return nums[begin];
    }
}




传送门:寻找旋转排序数组中的最小值 II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

Find the minimum element.

The array may contain duplicates.

假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

请找出其中最小的元素。

注意数组中可能存在重复的元素。

示例 1:
输入: [1,3,5]
输出: 1

示例 2:
输入: [2,2,2,0,1]
输出: 0


/**
 *
 * Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
 * (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]).
 * Find the minimum element.
 * The array may contain duplicates.
 * 假设按照升序排序的数组在预先未知的某个点上进行了旋转。
 * ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
 * 请找出其中最小的元素。
 * 注意数组中可能存在重复的元素。
 *
 */

public class FindMinimumInRotatedSortedArrayII {
    public int findMin(int[] nums) {
        int begin = 0;
        int end = nums.length - 1;
        while(begin < end){
            int mid = begin + ((end - begin) >> 1);
            if(nums[mid] < nums[begin]){
                end = mid;
            }else if(nums[end] < nums[begin]){
                begin = mid + 1;
            }
            //由于可能会出现重复元素,无法判断区间,例如[1,0,1,1,1]和[1,1,1,0,1],因此采用end--来规避问题
            else{
                end--;
            }
        }
        return nums[begin];
    }
}




#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值