当前位置:网站首页>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;
}
};
边栏推荐
- [leetcode] sword finger offer II 020 Number of palindrome substrings
- Huawei officially entered the "front loading" stage, and the millimeter wave radar track entered the "localization +4d" cycle
- Comprehensive analysis of C array
- 移动安全必备之CS呢【NETHUNTER】
- [Part VI] source code analysis and application details of countdownlatch [key]
- About three-tier architecture and MVC
- PyTorch常用参数初始化方法:【均匀分布、正态(高斯)分布、Xavier、kaiming、正交矩阵、稀疏矩阵、常数、单位矩阵、零填充】
- MYSQL 行转列、列转行、多列转一行、一行转多列
- Analysis report on production and marketing demand and investment forecast of China's Melamine Industry from 2022 to 2028
- iShot
猜你喜欢

Model over fitting - solution (II): dropout

Gb28181 protocol -- alarm

Chapter 8 - shared model JUC

Record 5 - the serial port of stm32f411ceu6 realizes the sending and receiving of fixed length data and variable length data

Web3 principle and decentralization

iShot

80 lines of code to realize simple rxjs

度量学习(Metric Learning)【AMSoftmax、Arcface】

Insight into China's smart medical industry in 2022

AWS lambda: how to store secrets to external APIs- AWS Lambda: How to store secret to external API?
随机推荐
[opencv learning] use the Tesseract OCR movement to recognize numbers
PostgreSQL 中文社区黑龙江分会和辽宁分会成立啦!
AWS lambda: how to store secrets to external APIs- AWS Lambda: How to store secret to external API?
Deep feature synthesis and genetic feature generation, comparison of two automatic feature generation strategies
Pytorch中的梯度累加【在实验时,由于GPU显存限制,遇到batch_size不能再增大的情况。为解决该问题,使用梯度累加方法】
Qrcodejs2 QR code generation JS
Coordinate transformation in pipelines
Flutter库推荐Sizer 可帮助您轻松创建响应式 UI
Find out the data that can match the keyword key in field 1 or field 2 in the database table. If you want to display the matching data in field 1 first
度量学习(Metric Learning)【AMSoftmax、Arcface】
启牛帮开通的股票账户是安全可信的吗?
Mgr and greatsql resource summary
Hongmeng starts 2
iShot
CST learning: four element array design of circular patch antenna (II) array formation and combination results
Face detection: mtcnn
Lua loop statement
CST learning: four element array design of circular patch antenna (III) array formation and parallel excitation
Redis realizes SMS verification code login
【LeetCode】300. Longest ascending subsequence