Description:

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.

随机找一个枢纽,旋转有序数组,找出最小元素。

O(n)解法,线性查找。1ms

public class Solution {
public int findMin(int[] nums) { int min = nums[0]; for(int i=1; i<nums.length; i++) {
if(min > nums[i])
min = nums[i];
} return min;
}
}

O(logn)解法,二分。0ms

public class Solution {
public int findMin(int[] nums) { int n = nums.length - 1; int left = 0, right = nums.length - 1; while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] > nums[n]) {
left = mid + 1;
}
else {
right = mid - 1;
}
} return nums[left];
}
}

LeetCode——Find Minimum in Rotated Sorted Array的更多相关文章

  1. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  2. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. [LeetCode] 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 migh ...

  4. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  5. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  6. Leetcode | 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 migh ...

  7. LeetCode——Find Minimum in Rotated Sorted Array II

    Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...

  8. 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 migh ...

  9. [LeetCode] 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 migh ...

随机推荐

  1. Apache HttpComponents 如何在正常结束前中止一个HTTP请求

    package org.apache.http.examples.client; import org.apache.http.HttpEntity; import org.apache.http.H ...

  2. [ES6]探究数据绑定之Proxy

    知识储备 Proxy 方式实现数据绑定中涉及到 Proxy.Reflect.Set.Map 和 WeakMap,这些都是 ES6 的新特性. Proxy Proxy 对象代理,在目标对象之前架设一层拦 ...

  3. CodeIgniter(3.1.4)框架使用静态文件(js,css)

    调整目录结构: 可以在控制器中这样加载视图: * 加载url辅助类. views视图中可以这样引用静态文件: 则最终的静态文件url会生成这样:

  4. 数据库 proc编程八

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  5. 普通windows版本安装winServer的特色功能 以dedup功能为展示点

    安装 Windows 功能角色 1.选择安装源 在 Windows 8.1 系统上不存在重复数据删除功能,需要从对应的服务器版本,即 Windows Server 2012 R2 上提取相关文件. 2 ...

  6. 关于jsp,javascript,php等语言

    技术一  jsp: java植入html   技术二 javascript(js)植入html   技术三早期php植入html 弱类型语言和强类型语言 弱类型语言无法实现函数重载,没办法

  7. SSH远程登录其他机器

    常用格式:ssh [-l login_name] [-p port] [user@]hostname更详细的可以用ssh -h查看. 不指定用户: ssh 192.168.0.11 指定用户: ssh ...

  8. 初次使用ets

    一.new(Name, Options) -> tid() | atom(),创建ets表. Options = [Option], 目测常用的属性, {keypos, Pos}:指定key的位 ...

  9. windows 下安装perl Tk 模块

    首先,安装activeperl ,安装过程中勾选自动添加PATH环境变量,这样安装后就不需要自己手动修改PATH环境变量: 通过cmd 调出命令行窗口,输入ppm ,然后回车,就开启了perl 的包管 ...

  10. SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。

    SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效.路径中具有非法字符. 右键编辑.... 打开并删除含有'<&l ...