- 浏览: 173329 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 331Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 450Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 510Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 429Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 619Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 427The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 386Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 517Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 529Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 370All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 554Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 763Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 735You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For 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 Median of Two Sorted Arrays H-Index II 暴力枚举法 Subsets Subsets II Permutations Permutations II Combinations Letter Combinations of a Phone Number 广度优先...
II 和 Find Minimum in Rotated Sorted Array这两道题目,我也是醉了。。。代码完全一样的不说,题目还都特别傻逼。 Maximum Product Subarray 动态规划,类似于求最大连续和,但这里由于是乘法,还要考虑符号问题。...
[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 ...
leetcode变形词 :fire: Leetcode / 数据结构和算法 ...in_Rotated_Sorted_Array.java) :pushpin: 数组 :pushpin: 比赛 提交 2020 年 Google Code Jam 资格赛 提交 Google Hash Code 2020 在线资格赛
find-minimum-in-rotated-sorted-array-0153 数组的乘积-除了-self-0238 从排序数组中删除重复项-0026 搜索旋转排序数组-0033 两个整数之和-0371 二和-0001 回溯 组合-和-0039 组合总和-ii-0040 电话号码的字母组合 ...
minimum number in array given a sorted array, rotated at a pivot, find the maximum element what is encapsulation(封装) 大部分时间在问general behavior question。 比如要是组里有人不工作怎么办, 工作如果...
- ADD: In the module FlexUtils added the constant BooleanWords - an array containing the words 'false' and 'true' for saving/reading TBoolProp. - ADD: In the module FlexUtils added the function ...