Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
public class Solution { public int[] searchRange(int[] nums, int target) { int[] res = {-1, -1}; if (nums == null || nums.length == 0) { return res; } int start = 0; int end = nums.length-1; int pos = 0; while (start <= end) { int mid = (start + end) / 2; pos = mid; if (target < nums[mid]) { end = mid - 1; } else if (target > nums[mid]) { start = mid + 1; } else { res[0] = pos; res[1] = pos; break; } } if (nums[pos] != target) { return res; } int newStart = pos; int newEnd = nums.length-1; while (newStart <= newEnd) { int mid = (newStart + newEnd) / 2; if (target == nums[mid]) { newStart = mid + 1; } else { newEnd = mid - 1; } } res[1] = newEnd; newStart = 0; newEnd = pos; while (newStart <= newEnd) { int mid = (newStart + newEnd) / 2; if (target == nums[mid]) { newEnd = mid - 1; } else { newStart = mid + 1; } } res[0] = newStart; return res; } }
相关推荐
用于电动汽车充电寻找最优路径,以及充电站的分布
Search for a Range Search Insert Position Search in Rotated Sorted Array Search in Rotated Sorted Array II Search a 2D Matrix Search a 2D Matrix II Find Minimum in Rotated Sorted Array Find Minimum in...
In a single data structure, the GiST provides all the basic search tree logic required by a database system, thereby unifying disparate structures such as B+-trees and R-trees in a single piece of ...
1. Introduction 2. Array i. Remove Element ii. Remove Duplicates from Sorted Array iii....iv....v....vi....vii....viii....ix....x.... Search for a Range xiii. Search Insert Position xiv. Find Peak Element
Monte Carlo Tree Search (MCTS) is a recently proposed search method that combines the precision of tree search with the generality of random sampling. It has received considerable interest due to its ...
Part I of the text describes major theoretical principles, and provides an extensive survey of specific techniques for a large range of applications. Part II concentrates on approaches particularly ...
Here's what you need―a highly practical guide that gives you a quick start with ElasticSearch using easy-to-follow examples; get up and running with ElasticSearch APIs in no time Get the latest ...
There is a wide range of Cognitive Services APIs available. This book focuses on some of the most useful and powerful ways that your application can make intelligent use of language. Artificial ...
* Implementation of a Ternary Search Trie, a data structure for storing <code>String</code> objects * that combines the compact size of a binary search tree with the speed of a digital search trie, ...
and local search strategies This allows for a broad range of operations In particular a solver suite approach supports the flexible usage of the component solvers: one can execute fully automatic ...
The optimal search cost was also found to be for a function that did not include any buffer zone. The optimal, average search cost across the whole sample was 11% of the defined search area. Fifty-...
Creating a Macro to Search for Specific Numeric Values 102 Working with Dates Introduction 105 Checking Ranges for Dates (Using a DATA Step) 106 Checking Ranges for Dates (Using PROC PRINT) 107 ...
We study a range of syntactic processing tasks using a general...choice for a range of syntactic processing tasks and one that should be considered for comparison by developers of alternative approaches.
<span xss=removed>Similarity search is a common computational task in many applications. Distance-based indexing ... In a range search task, objects which satisfy the inclusion rule also satisfy t
Throughout the book, the key search components of metaheuristics are considered as a toolbox for: Designing efficient metaheuristics (e.g. local search, tabu search, simulated annealing, ...
Chapter 6 presents a technique for approximate answering range-sum queries on data cubes. To this end, the authors propose tree-based data structures for separately storing sampled data and outliers ...
However, if we are searching for multiple rows, such as duplicate values, or keys in a range, anything more than a small number of rows will make the nonclustered index search very inefficient. ...
After testing a range of alternatives, we have found that multiple randomized k-d trees provide the best performance for other datasets. We are releasing public domain code that implements these ...
the users of their applications the ability to search or filter through their data using a regular expression. Regular expressions are everywhere. Many books have been published to ride the wave of ...
applied in a wide range domain areas : network management, electronic commerce, energy efficiency and metering; Wireless Multimedia Sensors, grid computing and grid services, distributed data mining, ...