- 浏览: 183555 次
- 性别:
- 来自: 济南
文章分类
最新评论
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
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.
The array may contain duplicates.
这道题目是[url] Find Minimum in Rotated Sorted Array[/url]的follow up, 唯一不同的是数组中多了重复元素。这时我们进行比较的时候就会出现相等的情况,如果相等我们就没法判断最小值是在哪半部分,我们只需要加一个相等时的处理,让左指针向前移动一个位置或右指针向后移动一个位置,直到找到不相等的两个元素,这样最坏的情况下就是元素都相等,时间复杂度为O(n)。代码如下:
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
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.
The array may contain duplicates.
这道题目是[url] Find Minimum in Rotated Sorted Array[/url]的follow up, 唯一不同的是数组中多了重复元素。这时我们进行比较的时候就会出现相等的情况,如果相等我们就没法判断最小值是在哪半部分,我们只需要加一个相等时的处理,让左指针向前移动一个位置或右指针向后移动一个位置,直到找到不相等的两个元素,这样最坏的情况下就是元素都相等,时间复杂度为O(n)。代码如下:
public class Solution { public int findMin(int[] nums) { if(nums == null || nums.length == 0) return -1; int l = 0; int r = nums.length - 1; while(l < r) { int m = l + (r - l) / 2; if(nums[m] > nums[r]) { l = m + 1; } else if(nums[m] < nums[r]) { r = m; } else { r --; } } return nums[l]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
Find Minimum In Rotated Sorted Array Find Minimum In Rotated Sorted Array II Search In Rotated Sorted Array Search In Rotated Sorted Array II 二分搜索/有序矩阵 Kth Smallest Element In A Sorted Matrix ...
sorted array 81: search in rotated sorted array ii 153: find minimum in rotated sorted array 162: find peak element 34: find first and last position of element in sorted array 300: longest increasing ...
- Find Minimum in Rotated Sorted Array II(在旋转排序数组中寻找最小值II) - Median(中位数) ### 分析与总结 该文档是针对LeetCode和LintCode平台上的算法题目的整理与解答,主要分为两大部分: 1. **...
javascript js_leetcode题解之154-find-minimum-in-rotated-sorted-array-ii.js
python python_leetcode题解之154_Find_Minimum_in_Rotated_Sorted_Array_II.py
II 和 Find Minimum in Rotated Sorted Array这两道题目,我也是醉了。。。代码完全一样的不说,题目还都特别傻逼。 Maximum Product Subarray 动态规划,类似于求最大连续和,但这里由于是乘法,还要考虑符号问题。...
javascript js_leetcode题解之153-find-minimum-in-rotated-sorted-array.js
python python_leetcode题解之153_Find_Minimum_in_Rotated_Sorted_Array.py
50. Find Minimum in Rotated Sorted Array II – With Duplicates:同上题,但在数组中有重复值。 这些知识点涵盖了编程面试中常见题型的解答方法与技巧,包括基础算法和数据结构的运用。掌握这些内容对通过技术...
50. Find Minimum in Rotated Sorted Array II – With Duplicates:与上题类似,但是数组中可能存在重复元素。 以上就是该文件中提及的知识点,都是与编程面试密切相关的经典算法和数据结构题目。通过学习和练习...
[Java](./src/Find minimum in Rotated Sorted Array II/Solution.java) 2014/10/20 难的 [Java](./src/在旋转排序数组中查找最小值/Solution.java) 2014/10/15 中等的 [Java](./src/最大乘积子阵列/Solution.java) ...
Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. ...
扔鸡蛋 leetcode LeetCode-Note-Mary Mary's leetcode notebook The order of problems ...in ...Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值) 2020/12/09 300. Longest Increasing
查找最小的旋转排序数组II 假设以升序排序的长度为n的数组在1到n之间旋转。 例如,数组nums = [0,1,4,4,5,6,7]可能变为: [4,5,6,7,0,1,4]如果旋转了4次。 [0,1,4,4,5,6,7]如果已旋转7次。 请注意,旋转数组[a ...
II 替换空格 从尾到头打印链表 重建二叉树 105. Construct Binary Tree from Preorder and Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in ...
Find Minimum in Rotated Sorted Array 双指针 题目 Solution Tag LintCode 604. Window Sum LeetCode 283. Moves Zeroes Array、Two Pointers Array、Two Pointers LeetCode 120. Triangle LintCode 382. Triangle ...
在本题解中,我们将深入探讨Python编程语言在解决LeetCode第153题——"寻找旋转排序数组中的最小值"(Find Minimum in Rotated Sorted Array)时的应用。LeetCode是一系列面向程序员的在线编程挑战平台,对于求职...
在LeetCode中,这个问题通常被称为“寻找旋转排序数组中的最小值”(Find Minimum in Rotated Sorted Array)。解决这个问题,我们可以利用数组的两个关键特性:一部分是升序排列,另一部分是降序排列。由于数组是...
如"寻找旋转排序数组中的最小值"(Find Minimum in Rotated Sorted Array)。 4. **滑动窗口**:在给定大小的窗口内处理数组元素,常用于求解最大/最小值、频率统计等问题。如"最宽的水平条"(Largest Rectangle in...