2200. Find All K-Distant Indices in an Array 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617181920212223242526class Solution {public: vector<int> findKDistantIndices(vector<int>& nums, int key, int k) { int n = nums.size(); vector<int> ret; vector<pair<int,int>> target; for(int i=0;i<n;++i){ if(nums[i]==key)target.push_back(make_pair(key, i)); } for(int i=0;i<n;++i){ if(nums[i] == key){ ret.push_back(i); } else{ for(pair p:target){ if(abs(i-p.second)<=k){ ret.push_back(i); break; } } } } return ret; }}; analysis time complexity O(n*mlogm) space complexity O(n)