当前位置:网站首页>Leetcode 2200. Find all k nearest neighbor subscripts in the array (yes, one pass)
Leetcode 2200. Find all k nearest neighbor subscripts in the array (yes, one pass)
2022-06-12 23:35:00 【I'm not xiaohaiwa~~~~】
I'll give you a subscript from 0 The starting array of integers nums And two integers key and k .K Nearest neighbor subscript yes nums A subscript in i , And satisfy that there is at least one subscript j bring |i - j| <= k And nums[j] == key .
To return as a list, press Increasing order Sort all of K Nearest neighbor subscript .
Example 1:
Input :nums = [3,4,9,1,3,9,5], key = 9, k = 1
Output :[1,2,3,4,5,6]
explain : therefore ,nums[2] == key And nums[5] == key .
- Subscript to subscript 0 ,|0 - 2| > k And |0 - 5| > k , So there is no j bring |0 - j| <= k And nums[j] == key . therefore 0 Is not a K Nearest neighbor subscript .
- Subscript to subscript 1 ,|1 - 2| <= k And nums[2] == key , therefore 1 It's a K Nearest neighbor subscript .
- Subscript to subscript 2 ,|2 - 2| <= k And nums[2] == key , therefore 2 It's a K Nearest neighbor subscript .
- Subscript to subscript 3 ,|3 - 2| <= k And nums[2] == key , therefore 3 It's a K Nearest neighbor subscript .
- Subscript to subscript 4 ,|4 - 5| <= k And nums[5] == key , therefore 4 It's a K Nearest neighbor subscript .
- Subscript to subscript 5 ,|5 - 5| <= k And nums[5] == key , therefore 5 It's a K Nearest neighbor subscript .
- Subscript to subscript 6 ,|6 - 5| <= k And nums[5] == key , therefore 6 It's a K Nearest neighbor subscript .
therefore , Returns... In ascending order [1,2,3,4,5,6] .
Example 2:
Input :nums = [2,2,2,2,2], key = 2, k = 2
Output :[0,1,2,3,4]
explain : Yes nums All subscripts of i , There is always a subscript j bring |i - j| <= k And nums[j] == key , So every subscript is a K Nearest neighbor subscript .
therefore , return [0,1,2,3,4] .
Tips :
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 1000
- key It's an array nums An integer in
- 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 146. LRU cache
- Shardingsphere-proxy-5.0.0 deployment table implementation (I)
- 测试平台系列(97) 完善执行case部分
- OpenCV源代码编译
- MySQL基础篇视图的总结
- Qrcodejs2 QR code generation JS
- The "fourteenth five year plan" development plan and panoramic strategic analysis report of China's information and innovation industry 2022 ~ 2028
- Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测
- China's new generation information technology industry "14th five year plan" special planning and innovation strategic direction report 2022 ~ 2028
- Modify the text color of the menu on the right of toobar
猜你喜欢
Opencv source code compilation
So, what is the difference between e.target and e.currenttarget?
设计消息队列存储消息数据的 MySQL 表格
2022年G3锅炉水处理考题模拟考试平台操作
度量学习(Metric Learning)【AMSoftmax、Arcface】
[opencv learning] perspective transformation matrix
C language: how to give an alias to a global variable?
2202-簡曆制作
[kubernetes guide ④] pod quick start
测试平台系列(97) 完善执行case部分
随机推荐
PHP删除二维数组中相同项的数据
Theory + practice will help you master the dynamic programming method
Face detection: mtcnn
C语言:如何给全局变量起一个别名?
Hongmeng starts 2
Go time format assignment
Lua conditional statement
Zhengzhou University of light industry -- development and sharing of harmonyos pet health system
Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测
Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication
Qrcodejs2 QR code generation JS
PostgreSQL 中文社区黑龙江分会和辽宁分会成立啦!
RT thread quick start - experience RT thread
妙才周刊 - 5
InfoQ geek media's 15th anniversary solicitation | brief introduction to the four challenges of building a micro service architecture
Report on the "fourteenth five year plan" and strategic strategy recommendations for China's intellectual property protection industry 2022 ~ 2028
〖Kubernetes指南④〗Pod快速入门
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
Abstract methods and abstract classes
2022 questions d'examen pour le personnel de gestion de la sécurité de l'unit é de gestion des produits chimiques dangereux et examen de simulation en ligne