topic034

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

README

在排序数组中查找元素的第一个和最后一个位置

1. 题目描述

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

如果数组中不存在目标值 target,返回 [-1, -1]。

进阶: 你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?

2. 示例

示例1

输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]

示例2

输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]

示例3

输入:nums = [], target = 0
输出:[-1,-1]

提示

  • 0 <= nums.length <= $10^5$
  • $-10^9$ <= nums[i] <= $10^9$
  • nums 是一个非递减数组
  • $-10^9$ <= target <= $10^9$

3. 解题

  1. 顺序查找

  2. 二分查找

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL