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的更多相关文章

  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(M)(P)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

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

  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. WebSockets基础

    网络套接字是下一代WEB应用程序双向通信技术,它是基于一个独立的socket并且需要客户端浏览器支持HTML5. 一旦你了解了网络套接字与WEB服务器的连接,你将可以从浏览器发送数据到服务器并且可以接 ...

  2. Altium Designer 2013 13 复制出错的问题

    刚换成Altium Designer 2013 13,谁知先碰了钉子,为了赶进度需要复制以前的一个原理图上的部分电路图,一复制尽然报错不能复制,通过百度和向高人求助,总结一下两种方法: 1.在电脑上虚 ...

  3. eclipse 工程乱码问题

    一 设置工作空间的编码格式(对新建的工程有效) window-->preference-->Gerneral-->Workspace-->Text file coding--- ...

  4. Android 自带图标库 android.R.drawable

    在xml文件中调用. android:title="@string/secure_connect"android:orderInCategory="100"an ...

  5. C语言:typedef 跟 define 的区别

    typedef (int*) pINT1;以及下面这行:#define pINT2 int* pINT1 a,b; 与pINT2 a,b; 定义的a,b 有差别吗 回答: typedef作为类型定义关 ...

  6. tableviewcell的取消选中,高亮

    1.取消多余cell的分割线 UIView *view = [UIView new]; view.backgroundColor = [UIColor clearColor]; [tableView ...

  7. Fix a corrupted user profile

    Fix a corrupted user profile Applies to Windows 7 Your user profile is a collection of settings that ...

  8. K Best(最大化平均数)_二分搜索

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  9. HttpWebRequest 和HttpWebResponse总结

    1. 总结 总结2 3. Code using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  10. MySQL 基本函数

    (1).字符串类 CHARSET(str) //返回字串字符集 CONCAT (string2 [,... ]) //连接字串 INSTR (string ,substring ) //返回subst ...