leetcode 153. Find Minimum in Rotated Sorted Array --------- java

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 array.

在一个反转了的排好序的数组中,找出最小的数

可以直接寻找。

public class Solution {
    public int findMin(int[] nums) {

        if( nums.length == 1)
            return nums[0];
        int result = nums[0];
        for( int i = 1 ; i<nums.length;i++)
            result = Math.min(nums[i],result);

        return result;
    }
}

二分查找。

public class Solution {
    public int findMin(int[] nums) {

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

            if( nums[left] == nums[mid] ){
                first = Math.min(Math.min(first,nums[left]),nums[right]);
                break;
            }
            else if( nums[left] < nums[mid] ){
                left = mid;
                mid = (left+right)/2;
                first = Math.min(first,nums[left]);
            }else {
                right = mid;
                mid = (left+right)/2;
                first = Math.min(first,nums[mid]);
            }

        }
        return Math.min(first,nums[left]);
    }
}
时间: 2024-10-14 12:43:32

leetcode 153. Find Minimum in Rotated Sorted Array --------- java的相关文章

Java for LeetCode 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 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 解题思路: 本题和Java for LeetCode 033 Search in Rotated S

【leetcode】Find Minimum in Rotated Sorted Array JAVA实现

一.题目描述 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 array. 二.分析 这题难度有,因为他默认的是数组中所有的元素是不同的.只有分为两种情况: 1.

153. Find Minimum in Rotated Sorted Array java solutions

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 array. Subscribe to see which companies asked this questi

LeetCode 153. Find Minimum in Rotated Sorted Array

二分查找. 因为在旋转前的数组是排好序了的, 所以当num[begin] > num[mid]时,表示我们要搜寻的最小数字在num[begin, ..., mid]之间: 反之,num[begin] < num[mid]时,表示我们要搜寻的最小数字在num[mid+1, ..., end]之间(没有被打乱的数组,如1,2,3,4,..,n这种情况除外,在下面代码中我们进行了特判). 例:考虑num = {5, 6, 7, 1, 2, 3, 4}, begin = 0, end = 6, mid

[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

&lt;LeetCode OJ&gt; Find Minimum in Rotated Sorted Array【153】

153. Find Minimum in Rotated Sorted Array My Submissions Question Total Accepted: 73048 Total Submissions: 209952 Difficulty: Medium 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

153. Find Minimum in Rotated Sorted Array - LeetCode

Question 153.?Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成[4,5,6,1,2,3],把1求出来 思路:遍历,如果当前元素比前一个元素小就是这个元素了 Java实现: public int findMin(int[] nums) { int ans = nums[0]; for (int i=0; i<nums.length; i++) { int pre

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

【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