- 浏览: 183565 次
- 性别:
- 来自: 济南
文章分类
最新评论
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
这道题目是Search in Rotated Sorted Array 的变形。在上一题中数组中没有重复元素,我们用二分法,通过对比每次都可以删掉一般的元素,那样搜索的时间复杂度为O(logn)。在这一道题目中存在重复元素,在判断target可能再哪个区间的时候,就需要多一个判断,如果中间的元素与最左边或最右边的元素相等的时候,我们要单独处理。假设我们用二分法查找,中间元素nums[m]和target不相等,这时我们应该判断target可能在哪个区间,通过中间元素nums[m]与最左边的元素nums[i]相比,如果nums[m]与nums[left]不相等,那么处理的过程和上一道题目一样,如果nums[m] == nums[left],我们可以让left指针向左移动一个位置,然后继续查找,知道遇到nums[m] != nums[left]的情况。这样如果一个长度为n的数组中所有的元素都相等,这时的时间复杂度就为O(n)。所以对于这道题的时间复杂度最坏情况为O(n)。代码如下:
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
这道题目是Search in Rotated Sorted Array 的变形。在上一题中数组中没有重复元素,我们用二分法,通过对比每次都可以删掉一般的元素,那样搜索的时间复杂度为O(logn)。在这一道题目中存在重复元素,在判断target可能再哪个区间的时候,就需要多一个判断,如果中间的元素与最左边或最右边的元素相等的时候,我们要单独处理。假设我们用二分法查找,中间元素nums[m]和target不相等,这时我们应该判断target可能在哪个区间,通过中间元素nums[m]与最左边的元素nums[i]相比,如果nums[m]与nums[left]不相等,那么处理的过程和上一道题目一样,如果nums[m] == nums[left],我们可以让left指针向左移动一个位置,然后继续查找,知道遇到nums[m] != nums[left]的情况。这样如果一个长度为n的数组中所有的元素都相等,这时的时间复杂度就为O(n)。所以对于这道题的时间复杂度最坏情况为O(n)。代码如下:
public class Solution { public boolean search(int[] nums, int target) { if(nums == null || nums.length == 0) return false; int l = 0; int r = nums.length - 1; while(l <= r) { int m = l + (r - l) / 2; if(target == nums[m]) return true; if(nums[m] < nums[l]) { if(target > nums[m] && target <= nums[r]) { l = m + 1; } else { r = m - 1; } } else if(nums[m] > nums[l]){ if(target >= nums[l] && target < nums[m]) { r = m - 1; } else { l = m + 1; } } else { l ++; } } return false; } }
发表评论
-
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 ...
相关推荐
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 Search A 2D ...
Search(二分查找) easy 69: 278: 35: 374: guess number higher or lower 349: intersection of two arrays 350: intersection of two arrays ii medium 33: search in sorted array 81: search in rotated sorted...
- Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array(在旋转排序数组中寻找最小值) - Find Minimum in Rotated Sorted Array II(在旋转排序数组中寻找...
Sorted Array 2 Remove Duplicates from Sorted Array II 3 Search in Rotated Sorted Array 4 Search in Rotated Sorted Array II 5 Median of Two Sorted Arrays 递归实现find kth 6 Longest Consecutive Sequence...
python python_leetcode题解之081_Search_in_Rotated_Sorted_Array_II
javascript js_leetcode题解之81-search-in-rotated-sorted-array-ii.js
c c语言_leetcode题解之0081_search_in_rotated_sorted_array_ii.zip
js js_leetcode题解之33-search-in-rotated-sorted-array.js
c语言入门 C语言_leetcode题解之33-search-in-rotated-sorted-array.c
- Search in Rotated Sorted Array II: 假设按照升序排序的数组在预先未知的某个点上进行了旋转,该问题要求在旋转后的数组中搜索特定元素。 这些知识点涵盖了数据结构和算法的核心概念,以及如何用Python语言实现...
第81题“搜索旋转排序数组II”(Search in Rotated Sorted Array II)是其中一个经典问题,它涉及到数组处理、二分查找以及边界条件的判断。本题解将深入探讨这个题目及其解决方案。 首先,我们要理解问题背景:...
- **2.1.4 Search in Rotated Sorted Array II** - 同上,但数组可能包含重复元素。 - **2.1.5 Median of Two Sorted Arrays** - 求两个有序数组的中位数。 - 实现思路:通过比较两个数组的中间值来逐步缩小...
- **在旋转排序数组中搜索 II/Search in Rotated Sorted Array II**: 当数组中存在重复元素时,在被旋转过的排序数组中搜索给定数字。 #### 高级算法技巧 - **数学与位操作(Math and Bit Manipulation)**: 利用数学...
Search in Rotated Sorted Array(搜索旋转排序数组)#数组 2020/12/08 19. Remove Nth Node From End of List(删除链表的倒数第N个节点) 153. Find Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值...
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 My Leetcode Problems ...Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last Position of Element in Sorted Array 在排序数组中查找元素的第一个和最后一个位
II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -> 2 26. Remove Duplicates from Sorted Array (E) 27. Remove Element (E) 31. Next Permutation (M) * -> index 주의, 부등호 하나 틀림 ...
Search in Rotated Sorted Array medium O 54 Spiral Matrix medium O 66 Plus One easy O O 118 Pascal's Triangle easy O O 119 Pascal's Triangle II easy O 要满足只用一个array大小空间O(k) k为input大小来完成...