Find Min In Rotated Sorted Array,寻找反转序列中最小的元素。

问题描述:寻找反转序列中最小的元素。

算法分析:和寻找某个数是一个道理,还是利用二分查找,总体上分两种情况。nums[left]<=nums[mid],else。但是,在截取子序列的时候,有可能得到一个顺序序列。如34512,截取后得到12,此时要对这种情况判断,因为是顺序的,所以,最左边的元素就是最小元素。这点区别于寻找target,因为寻找target是根据target和left,mid,right做比较判断的。所以就不用对这种顺序情况单独讨论了。

 1 public int findMin(int[] nums)
 2         {
 3             return findMin(nums, 0, nums.length - 1);
 4         }
 5         //递归
 6         public int findMin(int[] nums, int left, int right)
 7         {
 8             if(left == right)
 9             {
10                 return nums[left];
11             }
12             if(nums[left] < nums[right])//例如34512,在截取子序列时候,很可能就得到一个顺序序列,这时候直接判断。
13             {
14                 return nums[left];
15             }
16             int mid = (left+right)/2;
17             if(nums[left] < nums[mid])
18             {
19                 return findMin(nums, mid+1, right);
20             }
21             else
22             {
23                 return findMin(nums, left, mid);
24             }
25         }
26
27         //迭代
28         public int findMin2(int[] nums, int left, int right)
29         {
30             while(left <= right)
31             {
32                 if(nums[left] < nums[right])
33                 {
34                     return nums[left];
35                 }
36                 if(left == right)
37                 {
38                     return nums[left];
39                 }
40                 int mid = (left+right)/2;
41                 if(nums[left] <= nums[mid])
42                 {
43                     left = mid + 1;
44                 }
45                 else
46                 {
47                     right = mid;
48                 }
49             }
50             return -1;
51         }
时间: 2024-12-24 04:29:05

Find Min In Rotated Sorted Array,寻找反转序列中最小的元素。的相关文章

【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数

Add Date 2014-10-15 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 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the

Search in Rotated Sorted Array, 查找反转有序序列。利用二分查找的思想。反转序列。

问题描述:一个有序序列经过反转,得到一个新的序列,查找新序列的某个元素.12345->45123. 算法思想:利用二分查找的思想,都是把要找的目标元素限制在一个小范围的有序序列中.这个题和二分查找的区别是,序列经过mid拆分后,是一个非连续的序列.特别要注意target的上下限问题.因为是非连续,所以要考虑上下限,而二分查找,序列式连续的,只用考虑单限.有递归算法和迭代算法. 递归算法: 1 public int search(int[] nums, int target) 2 { 3 retu

Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode

递归 public class Solution { public int findMin(int[] num) { return helper(num, 0, num.length-1); } //with duplicate public static int helper(int[] a, int left, int right){ //one element if(left == right){ return a[left]; } //two elements if(left == ri

[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

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[154]-Find Minimum in Rotated Sorted Array II

Link: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-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

[leedcode 154] Find Minimum in Rotated Sorted Array 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

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

81. Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4