【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

今天看到LeetCode OJ题目下方多了“Show Tags”功能。我觉着挺好,方便刚開始学习的人分类练习。同一时候也是解题时的思路提示。

【题目】

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.

【解法】

题目比較简单,直接看代码吧,可是要想把代码写得美丽并不easy啊。

O(n)非常好写,O(lgn)要好好捋捋思路。

public class Solution {
    // O(n) simple
    public int findMin1(int[] num) {
        int len = num.length;
        if (len == 1) {
            return num[0];
        }

        for (int i = 1; i < len; i++) {
            if (num[i] < num[i-1]) {
                return num[i];
            }
        }

        return num[0];

        // 尼玛,看成找中间数了
        // if (len % 2 != 0) { //len is odd
        //     return num[(begin+len/2)%len];
        // } else {    //len is even
        //     return (num[(begin+len/2-1)%len] + num[(begin+len/2)%len]) / 2;
        // }
    }

    // O(lgn) not that good
    public int findMin2(int[] num) {
        int len = num.length;
        if (len == 1) return num[0];

        int left = 0, right = len-1;
        while (left < right) {
            if ((right-left) == 1) return Math.min(num[left], num[right]);

            if (num[left] <= num[right]) return num[left];

            int mid = (left + right) / 2;
            if (num[mid] < num[right]) {
                right = mid;
            } else if (num[left] < num[mid]) {
                left = mid;
            }
        }

        return num[left];
    }

    // O(lgn) optimized iteratively
    public int findMin3(int[] num) {
        int len = num.length;
        if (len == 1) return num[0];
        int left = 0, right = len-1;
        while (num[left] > num[right]) { // good idea
            int mid = (left + right) / 2;
            if (num[mid] > num[right]) {
                left = mid + 1;
            } else {
                right = mid; // be careful, not mid-1, as num[mid] maybe the minimum
            }
        }
        return num[left];
    }

    // O(lgn) optimized recursively
    public int findMin(int[] num) {
        return find(num, 0, num.length-1);
    }

    public int find(int[] num, int left, int right) {
        if (num[left] <= num[right]) {
            return num[left];
        }
        int mid = (left + right) / 2;
        if (num[mid] > num[right]) {
            return find(num, mid+1, right);
        }
        return find(num, left, mid);
    }
}
时间: 2024-10-24 12:02:04

【LeetCode】Find Minimum in Rotated Sorted Array 解题报告的相关文章

【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 d

LeetCode: Search in Rotated Sorted Array 解题报告

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 retu

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

leetcode -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

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

Leetcode#153Find Minimum in Rotated Sorted Array

Find Minimum in Rotated Sorted Array Total Accepted: 42341 Total Submissions: 127863My Submissions Question Solution 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

Leetcode#154Find Minimum in Rotated Sorted Array II

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

Leetcode Find Minimum in Rotated Sorted Array I and II

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. 在任何一个sublist中,如果头元素大于尾元素,那么这个minimum一定在这个sublist中间

[leetcode] Find Minimum in Rotated Sorted Array @ Python

source: https://oj.leetcode.com/problems/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 du