关闭

leetcode Find Minimum in Rotated Sorted Array

220人阅读 评论(0) 收藏 举报
分类:

leetcode Find Minimum in Rotated Sorted Array
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
这里写图片描述
题目的意思就是将一个有序数组进行旋转使得有序数组不部分有序 来查找这个有序数组中的最小值 这里采用的是log(n)的算法
l为左边界 r为右边届 m=l+(r-l)/2;
当 nums[m]>nums[l]时 l=m 在m的右边寻找最小值
当nums[m]

import java.util.*;
public class Solution {
    //code can solve the increase situation
    public  int findMin(int[] nums) {
        int l,r,m,min;
        l=0;
        r=nums.length-1;
        min=nums[0];
        while(l<=r)
        {
            m=l+(r-l)/2;
            if(nums[m]>nums[l])
            {
                if(nums[l]<min)
                    min=nums[l];
                l=m;
            }
            else if(nums[m]<nums[l])
            {
                if(nums[m]<min)
                    min=nums[m];
                r=m;
            }
            else
            {
                if(nums[l]<min)
                    min=nums[l];
                l++;
            }
        }
        return min;

    }
    /*
    public static void main(String args[]){
        int[] a = new int[2];
        Scanner reader = new Scanner(System.in);
        for(int i=0;i<2;i++)
            {      
                a[i]=reader.nextInt();
            }
        int min=findMin(a);
        System.out.print("min is"+min);
    }*/
}
1
0
查看评论

LeetCode(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 ...
  • feliciafay
  • feliciafay
  • 2015-01-21 09:10
  • 971

153.Find Minimum in Rotated Sorted Array [LeetCode Java实现]

题目链接:find-minimum-in-rotated-sorted-array 有待继续优化代码结构,但是已经AC了 /** * Suppose a sorted array is rotated at some pivot unknown to you beforehand. ...
  • ever223
  • ever223
  • 2015-03-19 19:03
  • 884

【LeetCode】【Solution】Find Minimum in Rotated Sorted Array I & 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). ...
  • ljiabin
  • ljiabin
  • 2014-11-10 22:25
  • 3086

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 ...
  • feliciafay
  • feliciafay
  • 2015-01-21 06:04
  • 1904

[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). ...
  • SunnyYoona
  • SunnyYoona
  • 2015-01-31 17:21
  • 1487

Leetcode Find Minimum in Rotated Sorted Array 题解

Leetcode Find Minimum in Rotated Sorted Array 题目大意,对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。 毫无疑问,遍历一次肯定可以找到,但这样时间复杂度是O(n),如果你在面试的...
  • xindoo
  • xindoo
  • 2014-10-22 09:08
  • 1203

Find Minimum in Rotated Sorted Array -- LeetCode

这道题是Search in Rotated Sorted Array的扩展,区别就是现在不是找一个目标值了,而是在bst中找最小的元素。主要思路还是跟Search in Rotated Sorted Array差不多,还是通过左边界和中间的大小关系来得到左边或者右边有序的信息,如果左半边有序,那么左...
  • linhuanmars
  • linhuanmars
  • 2014-10-25 07:11
  • 15961

Find Minimum in Rotated Sorted Array II -- LeetCode

这道题是Search in Rotated Sorted Array的扩展,思路在Find Minimum in Rotated Sorted Array中已经介绍过了,和Find Minimum in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现。不过正是因...
  • linhuanmars
  • linhuanmars
  • 2014-10-25 07:12
  • 13811

[C++]LeetCode: 80 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)...
  • cinderella_niu
  • cinderella_niu
  • 2015-01-09 10:56
  • 637

leetcode 153: Find Minimum in Rotated Sorted Array

Find Minimum in Rotated Sorted Array Total Accepted: 21207 Total Submissions: 65855 Suppose a sorted array is rotated at some pivot unknown to you...
  • xudli
  • xudli
  • 2014-12-30 04:30
  • 2108
    个人资料
    • 访问:109546次
    • 积分:4838
    • 等级:
    • 排名:第7031名
    • 原创:374篇
    • 转载:7篇
    • 译文:0篇
    • 评论:6条
    最新评论