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

This commit is contained in:
huangge1199 2021-04-09 10:13:03 +08:00
parent 1b57780940
commit 33abfefdf9
4 changed files with 136 additions and 2 deletions

View File

@ -0,0 +1,88 @@
//已知一个长度为 n 的数组预先按照升序排列经由 1 n 旋转 得到输入数组例如原数组 nums = [0,1,4,4,5,6,7] 在变
//化后可能得到
//
// 若旋转 4 则可以得到 [4,5,6,7,0,1,4]
// 若旋转 7 则可以得到 [0,1,4,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 = [1,3,5]
//输出1
//
//
// 示例 2
//
//
//输入nums = [2,2,2,0,1]
//输出0
//
//
//
//
// 提示
//
//
// n == nums.length
// 1 <= n <= 5000
// -5000 <= nums[i] <= 5000
// nums 原来是一个升序排序的数组并进行了 1 n 次旋转
//
//
//
//
// 进阶
//
//
// 这道题是 寻找旋转排序数组中的最小值 的延伸题目
// 允许重复会影响算法的时间复杂度吗会如何影响为什么
//
// Related Topics 数组 二分查找
// 👍 282 👎 0
package leetcode.editor.cn;
/**
* 154:寻找旋转排序数组中的最小值 II
*/
public class FindMinimumInRotatedSortedArrayIi {
public static void main(String[] args) {
//测试代码
Solution solution = new FindMinimumInRotatedSortedArrayIi().new Solution();
System.out.println(solution.findMin(new int[]{3, 3, 1, 3}));
}
/**
* 力扣代码
*/
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 if (nums[start] == nums[end]) {
start++;
} else {
end = current;
}
}
return Math.min(nums[start], nums[end]);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,46 @@
已知一个长度为 <code>n</code> 的数组,预先按照升序排列,经由 <code>1</code><code>n</code><strong>旋转</strong> 后,得到输入数组。例如,原数组 <code>nums = [0,1,4,4,5,6,7]</code> 在变化后可能得到:
<ul>
<li>若旋转 <code>4</code> 次,则可以得到 <code>[4,5,6,7,0,1,4]</code></li>
<li>若旋转 <code>7</code> 次,则可以得到 <code>[0,1,4,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 = [1,3,5]
<strong>输出:</strong>1
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [2,2,2,0,1]
<strong>输出:</strong>0
</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> 原来是一个升序排序的数组,并进行了 <code>1</code><code>n</code> 次旋转</li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>这道题是 <a href="https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/">寻找旋转排序数组中的最小值</a> 的延伸题目。</li>
<li>允许重复会影响算法的时间复杂度吗?会如何影响,为什么?</li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div>\n<div><li>👍 282</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