LeetCode——Find Minimum in Rotated Sorted Array
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的更多相关文章
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- 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 ...
- LeetCode——Find Minimum in Rotated Sorted Array II
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- 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 ...
- [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 ...
随机推荐
- Apache HttpComponents 如何在正常结束前中止一个HTTP请求
package org.apache.http.examples.client; import org.apache.http.HttpEntity; import org.apache.http.H ...
- [ES6]探究数据绑定之Proxy
知识储备 Proxy 方式实现数据绑定中涉及到 Proxy.Reflect.Set.Map 和 WeakMap,这些都是 ES6 的新特性. Proxy Proxy 对象代理,在目标对象之前架设一层拦 ...
- CodeIgniter(3.1.4)框架使用静态文件(js,css)
调整目录结构: 可以在控制器中这样加载视图: * 加载url辅助类. views视图中可以这样引用静态文件: 则最终的静态文件url会生成这样:
- 数据库 proc编程八
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...
- 普通windows版本安装winServer的特色功能 以dedup功能为展示点
安装 Windows 功能角色 1.选择安装源 在 Windows 8.1 系统上不存在重复数据删除功能,需要从对应的服务器版本,即 Windows Server 2012 R2 上提取相关文件. 2 ...
- 关于jsp,javascript,php等语言
技术一 jsp: java植入html 技术二 javascript(js)植入html 技术三早期php植入html 弱类型语言和强类型语言 弱类型语言无法实现函数重载,没办法
- SSH远程登录其他机器
常用格式:ssh [-l login_name] [-p port] [user@]hostname更详细的可以用ssh -h查看. 不指定用户: ssh 192.168.0.11 指定用户: ssh ...
- 初次使用ets
一.new(Name, Options) -> tid() | atom(),创建ets表. Options = [Option], 目测常用的属性, {keypos, Pos}:指定key的位 ...
- windows 下安装perl Tk 模块
首先,安装activeperl ,安装过程中勾选自动添加PATH环境变量,这样安装后就不需要自己手动修改PATH环境变量: 通过cmd 调出命令行窗口,输入ppm ,然后回车,就开启了perl 的包管 ...
- SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。
SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效.路径中具有非法字符. 右键编辑.... 打开并删除含有'<&l ...