当前位置:网站首页>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;
}
};
边栏推荐
- Unprecedented analysis of Milvus source code architecture
- Use js to listen for Keydown event
- It is meaningful to define genus, and D can use it to explain semantics
- 【LeetCode】300. Longest ascending subsequence
- Modify the text color of the menu on the right of toobar
- LeetCode —— 26. Remove duplicates from an ordered array
- Redis realizes SMS verification code login
- ShardingSphere-proxy-5.0.0部署之分表实现(一)
- Mgr and greatsql resource summary
- ImageView grayed, reflected, rounded, watermarked
猜你喜欢

Insight into China's smart medical industry in 2022
![[Part VI] source code analysis and application details of countdownlatch [key]](/img/6e/085e257c938a8c7b88c12c36df83e1.jpg)
[Part VI] source code analysis and application details of countdownlatch [key]

Comprehensive analysis of C array

Summary of MySQL foundation view

Using baserecyclerviewadapterhelper to implement tree structure

For product managers, which of the two certificates, PMP and NPDP, is more authoritative?

ShardingSphere-proxy-5.0.0部署之分表实现(一)

C language: how to give an alias to a global variable?

Anti aliasing / anti aliasing Technology

Huawei officially entered the "front loading" stage, and the millimeter wave radar track entered the "localization +4d" cycle
随机推荐
Ten key defensive points in the detailed attack and defense drill
About three-tier architecture and MVC
Use js to listen for Keydown event
The shutter library recommends sizer to help you easily create a responsive UI
Database system composition
Using baserecyclerviewadapterhelper to implement tree structure
[issue 30] shopee golang development experience
InfoQ geek media's 15th anniversary solicitation | brief introduction to the four challenges of building a micro service architecture
Unprecedented analysis of Milvus source code architecture
设计消息队列存储消息数据的 MySQL 表格
Function introduction and common terms of ZABBIX
【LeetCode】300. Longest ascending subsequence
Gradient accumulation in pytorch [during the experiment, due to the limitation of GPU video memory, the batch\u size can no longer be increased. To solve this problem, the gradient accumulation method
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
Alcohol detector based on 51 single chip microcomputer
Introduction to message oriented middleware (message queue)
Lua loop statement
Hostvars in ansible
PHP删除二维数组中相同项的数据
About three-tier architecture and MVC