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.


题解:就是找数组中第一个“下凹”的地方,可以遍历,不过也可以用二分法,速度更快。

特别注意边界的处理:如果当前的中间值mid在0这个位置,只要考虑它后面的元素是不是小于它;如果在数组最后的位置上,要考虑它前面的元素是不是大于它。最后当发现当前mid所指的值不是所求的“下凹点”的时候,要把它和最后数组的最后一位比较,如果它比数组的最后一位大,说明要找的“下凹点”在mid所指位置的后面,否则在mid所指位置的前面。

JAVA版本代码如下:

 import java.awt.datatransfer.StringSelection;

 public class Solution {
public static void main(String[] args){
int num[] = {3,4,5,1,2};
Solution s = new Solution();
System.out.println(s.findMin(num));
}
public int findMin(int[] num) {
int begin = 0;
int end = num.length-1;
while(begin <= end){
int mid = (begin + end)/2;
if(mid == 0){
if(mid+1 < num.length && num[mid+1] < num[mid])
return num[mid+1];
else {
return num[0];
}
}
if(mid == num.length-1){
if(mid-1 >=0 && num[mid-1] > num[mid])
return num[mid];
else {
return num[0];
}
}
if(num[mid-1] > num[mid] && num[mid+1] > num[mid])
return num[mid]; if(num[num.length-1] < num[mid])
begin = mid+1;
else {
end = mid-1;
} }
return num[0];
}
}

【leetcode刷题笔记】Find Minimum in Rotated Sorted Array的更多相关文章

  1. 【leetcode刷题笔记】Search in Rotated Sorted Array II

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

  2. 【leetcode刷题笔记】Search 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 ...

  3. 【leetcode刷题笔记】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

    简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

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

  6. LeetCode(153) Find Minimum in Rotated Sorted Array

    题目 Total Accepted: 65121 Total Submissions: 190974 Difficulty: Medium Suppose a sorted array is rota ...

  7. 【leetcode刷题笔记】Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. 【leetcode刷题笔记】Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. 【leetcode刷题笔记】Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  10. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

随机推荐

  1. IPOL图像处理分析经典在线(文献+源码)

    网址: IPOL Journal · Image Processing On Line https://www.ipol.im/ 分类: 搜索: 下载文献和源码: NLM算法:IPOL Journal ...

  2. caffe 网络参数设置

    weight_decay防止过拟合的参数,使用方式: 样本越多,该值越小 模型参数越多,该值越大 一般建议值: weight_decay: 0.0005 lr_mult, decay_mult 关于偏 ...

  3. [原创]Nexus5 移植OneStep

    OneStep 简介 https://github.com/SmartisanTech/android One Step 涉及的工程列表: frameworks_base (需要更改WindowMan ...

  4. c itoa和atoi

    #include <iostream> using namespace std; int main() { #if 1 ; ];//不要写成char*,因为没有分配空间 itoa(num, ...

  5. ios 统一设计,iOS6也玩扁平化

    转:http://esoftmobile.com/2014/01/14/build-ios6-ios7-apps/ 前言 前段时间,苹果在它的开发者网站上放出了iOS系统安装比例,其中iOS7占到78 ...

  6. [HackerRank] The Longest Common Subsequence

    This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...

  7. The Thinking of AutomaticTest(有关自动化测试的思考)

    考虑因素: 容易维护 简洁易懂 代码重用性好 系统的稳定性强 UI自动化:   数据的获取:装载的数据文件类型.数据的形式.数据的解析方法定义. 1.利用Junit单元测试组织用例,明确输入数据.预期 ...

  8. 转!!关于java类初始化顺序

    原文地址:http://www.cnblogs.com/luckygxf/p/4796955.html 1.没有继承 静态变量->静态初始化块->变量->变量初始化块->构造方 ...

  9. django ORM 批量操作:批量插入bulk_create

    django批量create数据:bulk_create(list实例) 项目中看到这样一句 models.表名.objects.using('数据库名').bulk_create(list实例) 其 ...

  10. Pandas 如何去除、取消已经设置好的索引

    Outline 今天处理数据时遇到一个问题: 因为业务需要,我对 df 进行了建立索引. 具体如下: 下面走的是默认索引 给其设置索引: 取消索引 业务需求,我要取消掉上面设置的索引: So,之前设置 ...