153:寻找旋转排序数组中的最小值

This commit is contained in:
huangge1199 2021-04-08 08:46:39 +08:00
parent 99c495ca04
commit 340f312293
4 changed files with 135 additions and 2 deletions

View File

@ -0,0 +1,85 @@
//已知一个长度为 n 的数组预先按照升序排列经由 1 n 旋转 得到输入数组例如原数组 nums = [0,1,2,4,5,6,7] 在变
//化后可能得到
//
// 若旋转 4 则可以得到 [4,5,6,7,0,1,2]
// 若旋转 4 则可以得到 [0,1,2,4,5,6,7]
//
//
// 注意数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2],
//..., a[n-2]]
//
// 给你一个元素值 互不相同 的数组 nums 它原来是一个升序排列的数组并按上述情形进行了多次旋转请你找出并返回数组中的 最小元素
//
//
//
// 示例 1
//
//
//输入nums = [3,4,5,1,2]
//输出1
//解释原数组为 [1,2,3,4,5] 旋转 3 次得到输入数组
//
//
// 示例 2
//
//
//输入nums = [4,5,6,7,0,1,2]
//输出0
//解释原数组为 [0,1,2,4,5,6,7] 旋转 4 次得到输入数组
//
//
// 示例 3
//
//
//输入nums = [11,13,15,17]
//输出11
//解释原数组为 [11,13,15,17] 旋转 4 次得到输入数组
//
//
//
//
// 提示
//
//
// n == nums.length
// 1 <= n <= 5000
// -5000 <= nums[i] <= 5000
// nums 中的所有整数 互不相同
// nums 原来是一个升序排序的数组并进行了 1 n 次旋转
//
// Related Topics 数组 二分查找
// 👍 395 👎 0
package leetcode.editor.cn;
//153:寻找旋转排序数组中的最小值
public class FindMinimumInRotatedSortedArray {
public static void main(String[] args) {
//测试代码
Solution solution = new FindMinimumInRotatedSortedArray().new Solution();
solution.findMin(new int[]{2, 1});
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findMin(int[] nums) {
int start = 0, end = nums.length - 1;
int current;
while (start < end - 1) {
current = (start + end) / 2;
if (nums[start] > nums[current]) {
start++;
end = current;
} else if (nums[current] > nums[end]) {
start = current;
} else {
end = current;
}
}
return Math.min(nums[start], nums[end]);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,48 @@
已知一个长度为 <code>n</code> 的数组,预先按照升序排列,经由 <code>1</code><code>n</code><strong>旋转</strong> 后,得到输入数组。例如,原数组 <code>nums = [0,1,2,4,5,6,7]</code> 在变化后可能得到:
<ul>
<li>若旋转 <code>4</code> 次,则可以得到 <code>[4,5,6,7,0,1,2]</code></li>
<li>若旋转 <code>4</code> 次,则可以得到 <code>[0,1,2,4,5,6,7]</code></li>
</ul>
<p>注意,数组 <code>[a[0], a[1], a[2], ..., a[n-1]]</code> <strong>旋转一次</strong> 的结果为数组 <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code></p>
<p>给你一个元素值 <strong>互不相同</strong> 的数组 <code>nums</code> ,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 <strong>最小元素</strong></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [3,4,5,1,2]
<strong>输出:</strong>1
<strong>解释:</strong>原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [4,5,6,7,0,1,2]
<strong>输出:</strong>0
<strong>解释:</strong>原数组为 [0,1,2,4,5,6,7] ,旋转 4 次得到输入数组。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [11,13,15,17]
<strong>输出:</strong>11
<strong>解释:</strong>原数组为 [11,13,15,17] ,旋转 4 次得到输入数组。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 5000</code></li>
<li><code>-5000 <= nums[i] <= 5000</code></li>
<li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
<li><code>nums</code> 原来是一个升序排序的数组,并进行了 <code>1</code><code>n</code> 次旋转</li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div>\n<div><li>👍 395</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long