题目:

Suppose an array sorted in ascending order 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.

思路:

1.题目中指出sorted array,find target使用二分法。

2.分为以下三种情况

(1)数组只有一个元素:nums[0]即为最小值。

(2)数组不是rotated sorted array,退化为sorted array:nums[0]即为最小值。

(3)数组是rotated sorted array:nums[start] > nums[end]:

a:nums[mid]大于nums[start].

b:nums[mid]小于nums[start].

代码:

 public class Solution {
public int findMin(int[] nums) {
int start = 0,end = nums.length-1,mid = 0;
//length equals 1;
if(nums.length == 1){
return nums[0];
}
//is rotated sorted array;
while(nums[start] > nums[end] && start + 1 < end){
mid = start + (end - start)/2;
if(nums[mid] > nums[start]){
start = mid;
}else if(nums[mid] < nums[start]){
end = mid;
}
}
//is not rotated sorted array;
if(nums[start] < nums[end]){
return nums[start];
}
return nums[end];
}
}

LeetCode 153.Find Minimum in Rotated Sorted Array(M)(P)的更多相关文章

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

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

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

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

  5. 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 migh ...

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

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

  8. 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. ( ...

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

随机推荐

  1. WEB-文件包含漏洞详解

    title date tags layout 文件包含漏洞(File Include) 2018-10-12 post 产生原因: 由于在编写代码时避免麻烦就需要把公用的一段代码写到一个单独的文件里面 ...

  2. android记帐本、涂鸦、仿腾讯新闻、仿bilibili、Markdwon便签、资讯APP等源码

    Android精选源码 kotlin版仿哔哩哔哩动画Android客户端源码 android实现图片涂鸦效果源码 Android 开源记账本项目源码 android高仿腾讯新闻app源码 androi ...

  3. why rpc

    why rpc 单体应用业务复杂, 大集群部署时对数据库的连接是个考验, 维护这个应用也比较费劲(一群人维护,容易代码冲突) 拆分后 各服务之间的接口依赖不能使用httpClient来搞,一是效率太低 ...

  4. [LC] 114. Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  5. 吴裕雄--天生自然 HADOOP大数据分布式处理:添加主机和服务器的域名映射

  6. 路由配置(route IP

    转载于:http://blog.csdn.net/chenlycly/article/details/52141854 使用下面的 route 命令可以查看 Linux 内核路由表. # route ...

  7. Jumpserver 一键部署(支持离线安装)

    1.教程介绍1.1::通过本教程起到抛砖引玉效果,希望各位喜爱Jumpserver堡垒机的朋友受益良多. 1.2::以下提供的任何软件仅供学习交流使用. 2.下载链接2.1::centos_1810最 ...

  8. python可变类型和不可变类型,深拷贝vs浅拷贝

    转载:https://www.cnblogs.com/huamingao/p/5809936.html 核心提示: 可变类型 Vs 不可变类型 可变类型(mutable):列表,字典 不可变类型(un ...

  9. [LC] 235. Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  10. 关于JavaScript中的==与!的转换问题

    最近遇到了一道很有趣的JavaScript试题,感觉很有趣.记录一下免得以后面试遇到 题目是: console.log([]==![],{}==!{},[]==!{},{}==![]) 这道题考察的主 ...