leetcode 153. Find Minimum in Rotated Sorted Array --------- java
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.
在一个反转了的排好序的数组中,找出最小的数
可以直接寻找。
public class Solution {
public int findMin(int[] nums) { if( nums.length == 1)
return nums[0];
int result = nums[0];
for( int i = 1 ; i<nums.length;i++)
result = Math.min(nums[i],result); return result;
}
}
二分查找。
public class Solution {
public int findMin(int[] nums) { if( nums.length == 1)
return nums[0];
int left = 0,right = nums.length-1;
int mid = (left+right)/2;
int first = nums[0];
while( left < right ){ if( nums[left] == nums[mid] ){
first = Math.min(Math.min(first,nums[left]),nums[right]);
break;
}
else if( nums[left] < nums[mid] ){
left = mid;
mid = (left+right)/2;
first = Math.min(first,nums[left]);
}else {
right = mid;
mid = (left+right)/2;
first = Math.min(first,nums[left]);
} }
return Math.min(first,nums[left]);
}
}
leetcode 153. Find Minimum in Rotated Sorted Array --------- java的更多相关文章
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- [LeetCode] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Java for 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 migh ...
- LeetCode 153.Find Minimum in Rotated Sorted Array(M)(P)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 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 migh ...
- LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Leetcode 153. Find Minimum in Rotated Sorted Array -- 二分查找的变种
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中的最小值 (C++)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- Android Phonebook编写联系人UI加载及联系人保存流程(一)
2014-01-06 17:05:11 将百度空间里的东西移过来. 本文适合ROM定制做Phonebook的童鞋看,其他人飘过即可- Phonebook添加/编辑联系人UI加载及保存联系人流程,是一系 ...
- Python的排序
1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I' ...
- 【模拟题(电子科大MaxKU)】解题报告【树形问题】【矩阵乘法】【快速幂】【数论】
目录: 1:一道简单题[树形问题](Bzoj 1827 奶牛大集会) 2:一道更简单题[矩阵乘法][快速幂] 3:最简单题[技巧] 话说这些题目的名字也是够了.... 题目: 1.一道简单题 时间1s ...
- IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果
问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...
- 戴文的Linux内核专题:04安全
转自Linux中国 Linux内核是所有Linux系统的核心.如果有任何恶意代码控制或破害了内核的任何一部分,那么系统会严重受损,文件可能被删除或损坏,私人信息可能被盗等等.很明显,保持内核安全涉及到 ...
- [vijos P1112] 小胖的奇偶
第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫.看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢? ...
- java获取页面编码
文章出自:http://babyjoycry.javaeye.com/blog/587527 在此感谢原作者...\(^o^)/~ 最近研究抓取网页内容,发现要获取页面的编码格式,Java没有现成 ...
- [处理器、单片机]ARM
1.ARM简介: ARM是Advanced RISC Machines的缩写.1985年4月26日,第一个ARM原型在英国剑桥的Acorn计算机有限公司诞生,由美国加州San Jose VLSI技术公 ...
- [转][C/C++]函数名字修饰(Decorated Name)方式
1.C/C++函数修饰名: 对于我们的C/C++源程序而言,函数名只是函数的一小部分,函数还有调用方式(参数入栈方式).返回值类型.参数个数和各参数类型等信息,对于C++类成员函数,还有更多信息.这些 ...
- 静态方法被override
其实这并不是真正意义上的java override,因为如果在子类的方法上面加上@override编译不通过 而且如果使用父类引用指向子类实例,那么调用被改写的子类和父类都有的静态方法,执行的还是父类 ...