【每日一算】旋转有序数组

在旋转有序数组中找出给定的一个整数,并返回该整数在数组中的下标?

//常规有序数组
int[] arr1 = {1,2,3,4,5}
//旋转有序数组
int[] arr2 = {50,60,70,80,20,30,40} 

解题思路:

  1. 假设最左边下标用left标识,最右边下标有right标识,中间整数下标用mid标识;
  2. 每次判断下标mid对应的整数值是否大于下标left对应整数值,如果大于,说明左边有序,如果小于,说明右边有序;
  3. 在上一步的基础上,如果左边有序,判断目标值target实在左边还是在右边,如果在左边,right = mid-1,否则 left = mid+1;
  4. 如果右边有序,判断目标值target在左边还是右边,如果在右边,left = mid+1,否则 right = mid -1

代码如下:

public class Solution_2 {

    private static int findTargetIndex(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid;
        while (left <= right) {
            mid = (right - left) / 2 + left;
            if (nums[mid] == target) {
                return mid;
            }

            if (nums[mid] > nums[left]) {
                //说明在左侧有序
                if (target >= nums[left] && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                //说明在右侧有序
                if (target > nums[mid] && target <= nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 1, 2, 3, 4, 5, 6,7};
        int target = 2;
        int targetIndex = findTargetIndex(arr, target);
        System.out.println("targetIndex = " + targetIndex);
    }
}

原文地址:https://www.cnblogs.com/it-cj/p/11399329.html

时间: 2024-10-22 12:53:17

【每日一算】旋转有序数组的相关文章

(算法)旋转有序数组中查找某个数

题目: 假设有个有序数组在某个位置旋转,得到新的数组,即为旋转有序数组.如:(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 现给定一个这样的数组,在数组中查找某个数.如果找到,返回下标,否则返回-1: 思路: 思路1: 考虑一个旋转有序数组的特点:前面部分是递增的,后面部分也是递增的,即先后两部分都为递增有序数组,因此可以用二分查找来做. 假设数组为A,下标从left到right,查找的数字为target,那么mid=(left+((right

[LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are 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 7 might be

旋转有序数组的二分查找

要求 给定一个没有重复元素的旋转数组(它对应的原数组是有序的),求给定元素在旋转数组内的下标(不存在的返回-1). 例子 有序数组{0,1,2,3,4,5,6,7}对应的旋转数组为{3,4,5,6,7,0,1,2}(左旋.右旋效果相同). 查找元素5,返回结果2: 查找元素8,返回结果-1. 分析 可以轻易地想到遍历一遍O(n)时间可以得到结果,但是并不是最好的结果:利用有序的特点,可以轻易的想到二分查找的方法.经过旋转后的数组总是可以分为两个有序序列,如下图所示.旋转数组分成了红蓝两段有序序列

Find Minimum in Rotated Sorted Array 2 寻找旋转有序数组的最小值之二

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are 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 7 might be

[LeetCode] 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 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 duplic

[Leetcode] 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

求旋转有序数组的最小值和在旋转数组中查找

#include<iostream> using namespace std; int find2(int A[],int n) { int high=n-1; int low =0; int mid; while(A[high]<=A[low]) { if(high-low==1) { mid=high; break; } mid=low+(high-low)/2; if(A[mid]>=A[low]) { low=mid; }else if(A[mid]<=A[high]

[CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. EXAMPLE Input: find 5 in {15, 16, 19, 20, 2

[LeetCode] 33. 搜索旋转排序数组 ☆☆☆(二分查找)

描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [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], ta