[Leetcode] Find Minimum in rotated Sorted Array

Source: Internet
Author: User

Test instructions: Finding the smallest element from a translated array

Idea 1: The simplest idea is to sort and then take the first element, the complexity O (N*log (N))
Code Listing 1:

public class Solution {public    int findmin (int[] num) {        arrays.sort (num);        return num[0];}    }

Idea 2: Traverse, compare the first of the two orderby ordinal numbers, 4 5 6 7 0 1 2 complexity O (N)
Code Listing 2:

    public int findmin (int[] num) {//solution 1 times        int min = num[0], currmin = num[0];        int i = 0;        while (+ + i < num.length) {            currmin = math.min (num[i],currmin);            if (i + 1 < num.length && Num[i + 1] < Currmin) {                currmin = num[i + 1];                break;            }        }        min = math.min (min, currmin);        return min;    }
Train of thought 3: dichotomy, Time complexity O (log (N))
4 5 670 1 2 mid > R//l= mid +1
4 5 601 2 3 Mid < R//R = Mid
Code:
    public int findmin (int[] num) {//solution 32 min        int min = 0;        int L = 0, R = num.length-1, mid;        while (L < R) {            mid = L + (r-l)/2;            if (Num[mid] > Num[r]) {                L = mid + 1;            } else {                r = Mid;            }        }        min = num[l];        return min;    }



[Leetcode] Find Minimum in rotated Sorted Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.