当前位置:网站首页>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;
}
};
边栏推荐
- C language: how to give an alias to a global variable?
- [Part 7] source code analysis and application details of cyclicbarrier [key]
- The programmer has worked for 7 years. At the age of 31, he has no choice but to deliver takeout. I really don't want you to go through his journey again
- 【LeetCode】33. Search rotation sort array
- Anti aliasing / anti aliasing Technology
- CS for mobile security [nethunter]
- Modify the text color of the menu on the right of toobar
- [issue 30] shopee golang development experience
- ASP. Net core Middleware
- iShot
猜你喜欢
[Part 7] source code analysis and application details of cyclicbarrier [key]
[kubernetes guide ④] pod quick start
Industry reshuffle, a large number of programmers are going to lose their jobs? How can we break the current workplace dilemma
Photoshop:ps how to enlarge a picture without blurring
Common message oriented middleware selection
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
2022 heavyweight: growth law - skillfully use digital marketing to break through enterprise difficulties
Coordinate transformation in pipelines
Zhengzhou University of light industry -- development and sharing of harmonyos pet health system
Web3 principle and decentralization
随机推荐
Lua conditional statement
Dry goods sharing | BitSet application details
Common message oriented middleware selection
Leetcode1601: the maximum number of building change requests that can be reached (difficult)
Sword finger offer series - 47 Maximum value of gifts
Theory + practice will help you master the dynamic programming method
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
MOOG servo valve d634-341c/r40ko2m0nss2
The Milvus graphical management tool Attu is coming!
Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测
Analysis report on business model innovation path and operation status of China's app store industry from 2022 to 2028
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
Ten key defensive points in the detailed attack and defense drill
ASP. Net core Middleware
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
細數攻防演練中十大關鍵防守點
【LeetCode】5. Longest Palindromic Substring
The "fourteenth five year plan" development plan and panoramic strategic analysis report of China's information and innovation industry 2022 ~ 2028
C language: how to give an alias to a global variable?
Mgr and greatsql resource summary