当前位置:网站首页>Leetcode 2200. 找出数组中的所有 K 近邻下标(可以,一次过)
Leetcode 2200. 找出数组中的所有 K 近邻下标(可以,一次过)
2022-06-12 23:30:00 【我不是萧海哇~~~~】

给你一个下标从 0 开始的整数数组 nums 和两个整数 key 和 k 。K 近邻下标 是 nums 中的一个下标 i ,并满足至少存在一个下标 j 使得 |i - j| <= k 且 nums[j] == key 。
以列表形式返回按 递增顺序 排序的所有 K 近邻下标。
示例 1:
输入:nums = [3,4,9,1,3,9,5], key = 9, k = 1
输出:[1,2,3,4,5,6]
解释:因此,nums[2] == key 且 nums[5] == key 。
- 对下标 0 ,|0 - 2| > k 且 |0 - 5| > k ,所以不存在 j 使得 |0 - j| <= k 且 nums[j] == key 。所以 0 不是一个 K 近邻下标。
- 对下标 1 ,|1 - 2| <= k 且 nums[2] == key ,所以 1 是一个 K 近邻下标。
- 对下标 2 ,|2 - 2| <= k 且 nums[2] == key ,所以 2 是一个 K 近邻下标。
- 对下标 3 ,|3 - 2| <= k 且 nums[2] == key ,所以 3 是一个 K 近邻下标。
- 对下标 4 ,|4 - 5| <= k 且 nums[5] == key ,所以 4 是一个 K 近邻下标。
- 对下标 5 ,|5 - 5| <= k 且 nums[5] == key ,所以 5 是一个 K 近邻下标。
- 对下标 6 ,|6 - 5| <= k 且 nums[5] == key ,所以 6 是一个 K 近邻下标。
因此,按递增顺序返回 [1,2,3,4,5,6] 。
示例 2:
输入:nums = [2,2,2,2,2], key = 2, k = 2
输出:[0,1,2,3,4]
解释:对 nums 的所有下标 i ,总存在某个下标 j 使得 |i - j| <= k 且 nums[j] == key ,所以每个下标都是一个 K 近邻下标。
因此,返回 [0,1,2,3,4] 。
提示:
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 1000
- key 是数组 nums 中的一个整数
- 1 <= k <= nums.length
Code:
class Solution {
public:
vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
vector<int>pos;
for(int i=0;i<nums.size();i++)
{
if(nums[i]==key)
{
pos.push_back(i);
}
}
vector<int>res;
for(int i=0;i<nums.size();i++)
{
for(int j=0;j<pos.size();j++)
{
if(abs(i-pos[j])<=k)
{
res.push_back(i);
break;
}
}
}
sort(res.begin(),res.end());
return res;
}
};
边栏推荐
- Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication
- Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
- Theory + practice will help you master the dynamic programming method
- Deep learning neural network: implementation method of convolution [direct method (no loss of precision), GEMM (matrix multiplication, no loss of precision), FFT (Fourier transform, loss of precision)
- Leetcode 890 finding and replacing patterns [map] the leetcode path of heroding
- 设计消息队列存储消息数据的 MySQL 表格
- Mgr and greatsql resource summary
- Pytorch common parameter initialization methods: [uniform distribution, normal (Gaussian) distribution, Xavier, Kaiming, orthogonal matrix, sparse matrix, constant, identity matrix, zero filling]
- [Part 7] source code analysis and application details of cyclicbarrier [key]
- The carrying capacity of L2 level ADAS increased by more than 60% year-on-year in January, and domestic suppliers "emerged"
猜你喜欢

80 lines of code to realize simple rxjs

Chapter 8 - shared model JUC

PostgreSQL 中文社区黑龙江分会和辽宁分会成立啦!

Embedded pipeline out of the box

OpenCV源代码编译

设计消息队列存储消息数据的 MySQL 表格

Introduction to message oriented middleware (message queue)

Redis实现短信验证码登录

2202 - production de CV

RT thread quick start - experience RT thread
随机推荐
The Milvus graphical management tool Attu is coming!
测试平台系列(97) 完善执行case部分
Qrcodejs2 QR code generation JS
Database system composition
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
Sequence maximum return
[opencv learning] use the Tesseract OCR movement to recognize numbers
Unprecedented analysis of Milvus source code architecture
Web3 principle and decentralization
House raiding 2
Coordinate transformation in pipelines
Function introduction and common terms of ZABBIX
Industry reshuffle, a large number of programmers are going to lose their jobs? How can we break the current workplace dilemma
Analysis report on business model innovation path and operation status of China's app store industry from 2022 to 2028
About three-tier architecture and MVC
[North Asia data recovery] data recovery cases in which the partitions disappear and the partitions are inaccessible after the server reinstalls the system
Analysis report on the 14th five year development plan and operation mode of China's hazardous waste treatment industry from 2022 to 2028
DETR(Detection with Transformers) 学习笔记
Heilongjiang Branch and Liaoning Branch of PostgreSQL Chinese community have been established!
[leetcode] the k-largest element in the array