今天看到LeetCode OJ题目下方多了“Show Tags”功能。我觉着挺好,方便刚開始学习的人分类练习。同一时候也是解题时的思路提示。

【题目】

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.

【解法】

题目比較简单,直接看代码吧,可是要想把代码写得美丽并不easy啊。

O(n)非常好写,O(lgn)要好好捋捋思路。

public class Solution {
// O(n) simple
public int findMin1(int[] num) {
int len = num.length;
if (len == 1) {
return num[0];
} for (int i = 1; i < len; i++) {
if (num[i] < num[i-1]) {
return num[i];
}
} return num[0]; // 尼玛,看成找中间数了
// if (len % 2 != 0) { //len is odd
// return num[(begin+len/2)%len];
// } else { //len is even
// return (num[(begin+len/2-1)%len] + num[(begin+len/2)%len]) / 2;
// }
} // O(lgn) not that good
public int findMin2(int[] num) {
int len = num.length;
if (len == 1) return num[0]; int left = 0, right = len-1;
while (left < right) {
if ((right-left) == 1) return Math.min(num[left], num[right]); if (num[left] <= num[right]) return num[left]; int mid = (left + right) / 2;
if (num[mid] < num[right]) {
right = mid;
} else if (num[left] < num[mid]) {
left = mid;
}
} return num[left];
} // O(lgn) optimized iteratively
public int findMin3(int[] num) {
int len = num.length;
if (len == 1) return num[0];
int left = 0, right = len-1;
while (num[left] > num[right]) { // good idea
int mid = (left + right) / 2;
if (num[mid] > num[right]) {
left = mid + 1;
} else {
right = mid; // be careful, not mid-1, as num[mid] maybe the minimum
}
}
return num[left];
} // O(lgn) optimized recursively
public int findMin(int[] num) {
return find(num, 0, num.length-1);
} public int find(int[] num, int left, int right) {
if (num[left] <= num[right]) {
return num[left];
}
int mid = (left + right) / 2;
if (num[mid] > num[right]) {
return find(num, mid+1, right);
}
return find(num, left, mid);
}
}

【LeetCode】Find Minimum in Rotated Sorted Array 解题报告的更多相关文章

  1. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  2. LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  3. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  4. Leetcode Find Minimum in Rotated Sorted Array 题解

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

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

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

  6. [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 ...

  7. LeetCode Find Minimum in Rotated Sorted Array II

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

  8. LeetCode Find Minimum in Rotated Sorted Array

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

  9. 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 ...

随机推荐

  1. 【“10”力全开 游戏“Ti”厉害】ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB)

    [“10”力全开 游戏“Ti”厉害]ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB) http: ...

  2. 智联招聘的python岗位数据词云制作

    # 根据传入的背景图片路径和词频字典.字体文件,生成指定名称的词云图片 def generate_word_cloud(img_bg_path, top_words_with_freq, font_p ...

  3. [ Python - 13 ] 批量管理主机必备模块

    批量管理程序必备模块 optparse configparser paramiko optparse模块 简介:        optparse模块主要用来为脚本传递命令参数功能 使用步骤: 1. i ...

  4. 【SQL】视图

    一.虚拟视图 由create table定义的表:以物理形式存在,实际存储在数据库中 视图:虚拟的,并不是一个真正存在的表 1.视图定义 CREATE VIEW <视图名> AS < ...

  5. Selenium2+python自动化69-PhantomJS使用【转载】

    前言 PhantomJS是一个没有界面的浏览器,本质上是它其实也就是一个浏览器,只是不在界面上展示. PhantomJS非常适合爬虫方面,很多玩爬虫的都喜欢用这个浏览器. 一.PhantomJS环境准 ...

  6. DB2、ORACLE SQL写法的主要区别

    DB2.ORACLE SQL写法的主要区别   说实话,ORACLE把国内的程序员惯坏了,代码中的SQL充斥着大量ORACLE特性,几乎没人知道ANSI的标准SQL是什么样子,导致程序脱离了ORACL ...

  7. python提纲

    根据网上专栏整理提纲 1.模块介绍 2.time&datetime模块 3.random模块 4.os模块 5.sys模块 6.json&pickle模块 7.logging模块 8. ...

  8. Codeforces 626 A. Robot Sequence (8VC Venture Cup 2016-Elimination Round)

      A. Robot Sequence   time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. JavaScript代码放在head和body的区别(QRCode生成)

    1.在head中时,所代表的functions只加载而不执行,执行是在某一事件触发后才开始. 2.在body中时,直接加载并执行 典型的区别: 如果有不在函数中的执行语句,比如变量初始化,如果在hea ...

  10. python的函数定义中99%的人会遇到的一个坑

    列表是一种经常使用的数据类型.在函数的定义中,常常会使用列表作为参数. 比如,要测试一个接口的数据,接口返回的数据格式如下: { "code": "20000" ...