leetcode(153 154) FindMinimuminRotatedSortedArray与FindMinimuminRotatedSortedArray II
来源:互联网 发布:linux查看crontab任务 编辑:程序博客网 时间:2024/05/12 18:46
链接:
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/
两道题的唯一不同点就是有无元素重复
用二分法解.重复情况下仅仅需要多写个else就好了
第一题java代码:
package leetcode;public class FindMinimuminRotatedSortedArray {// https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/// 元素无重复排序// 二分法public static int findMin(int[] nums) {if (nums == null || nums.length == 0)return 0;int l = 0;int r = nums.length - 1;int min = nums[0];while (l < r - 1) {int m = (l + r) / 2;if (nums[l] < nums[m]) {min = Math.min(nums[l], min);l = m + 1;} else{min = Math.min(nums[m], min);r = m - 1;} }min = Math.min(nums[r], min);min = Math.min(nums[l], min);return min;}public static void main(String[] args) {// TODO Auto-generated method stubint[] nums = { 5, 6, 7, 8, 9, 10, 3 };System.out.println(findMin(nums) + "");}}第二题java代码:
package leetcode;public class FindMinimuminRotatedSortedArrayII {// https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/// 元素有重复排序// 与FindMinimuminRotatedSortedArray不同在于有重复,这是只要多写一个判断条件即可,让左边自加1// 二分法public static int findMin(int[] nums) {if (nums == null || nums.length == 0)return 0;int l = 0;int r = nums.length - 1;int min = nums[0];while (l < r - 1) {int m = (l + r) / 2;if (nums[l] < nums[m]) {min = Math.min(nums[l], min);l = m + 1;} else if (nums[l] > nums[m]) {min = Math.min(nums[m], min);r = m - 1;} else {<span style="white-space:pre"></span>//如果相等,左边自加一次l++;}}min = Math.min(nums[r], min);min = Math.min(nums[l], min);return min;}public static void main(String[] args) {// TODO Auto-generated method stubint[] nums = { 5, 6, 7, 8, 9, 10, 3 };System.out.println(findMin(nums) + "");}}
0 0
- leetcode(153 154) FindMinimuminRotatedSortedArray与FindMinimuminRotatedSortedArray II
- Leetcode:Permutations与Permutations II
- Leetcode中字符串与模式匹配II
- Leetcode中一维数组与应用II
- Leetcode:Spiral Matrix与Spiral Matrix II
- Leetcode:Combination Sum与Combination Sum II
- Leetcode:Path Sum与Path Sum II
- [Leetcode 153 && 154] Find Minimum in Rotated Sorted Array (II)
- 数据结构与算法[LeetCode]——Jump Game I II
- 数据结构与算法 LeetCode编程练习--Contains Duplicate II
- [LeetCode] Linked List Cycle 与 Linked List Cycle II
- [LeetCode] Unique Paths II 起点与终点确定的路径数II
- !leetcode[153 & 154]:Find Minimum in Rotated Sorted Array I & II
- Leetcode 153, 154 Find Minimum in Rotated Sorted Array I, II
- LeetCode: Combination Sum II
- LeetCode: Combination Sum II
- LeetCode: Jump Game II
- LeetCode: Unique Paths II
- poj1141 2010.7.31
- poj1191 2010.7.31
- 318. Maximum Product of Word Lengths LeetCode
- 144. Binary Tree Preorder Traversal LeetCode
- 《Storm入门》中文版
- leetcode(153 154) FindMinimuminRotatedSortedArray与FindMinimuminRotatedSortedArray II
- poj1390 2010.8.1
- 94. Binary Tree Inorder Traversal LeetCode
- 不容易系列之(3)—— LELE的RPG难题
- poj2231 2010.8.1
- POJ 2771 Guardian of Decency(二分图最大独立集)
- 1026. 程序运行时间
- NYOJ-71-独木舟上的旅行
- uva10617 2010.8.1