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 ...
随机推荐
- EFM32 DMA/PRS例程
/**************************************************************************//** * @file * @brief H ...
- jqueryEasyui常用代码
//查询: function doSearch(form){ var fields =$('#queryForm').serializeArray(); var $fm = $(form); var ...
- 第三百一十三节,Django框架,Session
第三百一十三节,Django框架,Session Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 1.数据库(默认)2.缓存3.文件4.缓存+数据库5.加密c ...
- H.264:FFMpeg 实现简单的播放器
H.264:FFMpeg 实现简单的播放器 FFMPEG工程浩大,可以参考的书籍又不是很多,因此很多刚学习FFMPEG的人常常感觉到无从下手.我刚接触FFMPEG的时候也感觉不知从何学起. 因此我 ...
- 双卡手机怎么指定SIM卡打电话
双卡手机如何指定SIM卡打电话 package com.example.dualsimtest; import android.app.Activity; import android.content ...
- LogCat大量Unexpected value from nativeGetEnabledTags: 0
在执行模拟器的时候.LogCat 输出非常多Unexpected value from nativeGetEnabledTags: 0 提示.导致非常多本来须要输出的信息被瞬间覆盖了,查询后得知是sd ...
- erlang-百度云推送Android服务端功能实现-erlang
百度云推送官方地址http://developer.baidu.com/wiki/index.php?title=docs/cplat/push 简单的介绍下原理: 百度云推送支持IOS和Androi ...
- ThinkPHP中调用PHPExcel
//引入PHPExcel vendor('PHPExcel.PHPExcel'); // Create new PHPExcel object $objPHPExcel = new PHPExcel( ...
- TTreeView TTreeNodes TTreeNode
TTreeView 填写 TTreeView 的内容一般是这样开始的(下图), 不过我觉得最好习惯用动态建立. 打个比方: 譬如 TreeView 是一个军营的"营部"! 这里会有 ...
- 怎么用MathType编辑带圈字母
在用word公式编辑器MathType编辑公式时,里面涉及到很多的数学物理符号等等,这些符号或者是公式其实都可以利用MathType中的模板进行组合.在这些数学公式中,有时会有一些比较特殊的符号,它们 ...