problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::shortest_distance_to_target_color::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< int > shortestDistanceColor (vector< int > &colors, vector< vector< int > > &queries)
 

详细描述

在文件 leetcode.h2761 行定义.

成员函数说明

◆ shortestDistanceColor()

vector< int > leetcode::shortest_distance_to_target_color::Solution::shortestDistanceColor ( vector< int > &  colors,
vector< vector< int > > &  queries 
)
static

在文件 leetcode.cpp7572 行定义.

7572 {
7573 vector<int> ans;
7574 unordered_map<int, vector<int>> um;
7575 for(int i = 0; i < colors.size(); i++) {
7576 um[colors[i]].emplace_back(i);
7577 }
7578 for(auto &[k, vec]: um) {
7579 sort(vec.begin(), vec.end());
7580 }
7581 for(auto &query: queries) {
7582 int &i = query[0];
7583 int &c = query[1];
7584 vector<int> &vec = um[c];
7585 auto g = lower_bound(vec.begin(), vec.end(), i, less());
7586 auto l = lower_bound(vec.rbegin(), vec.rend(), i, greater());
7587 if(g == vec.end() && l == vec.rend()) {
7588 ans.emplace_back(-1);
7589 } else if(g == vec.end()) {
7590 ans.emplace_back(abs(*l - i));
7591 } else if(l == vec.rend()) {
7592 ans.emplace_back(abs(*g - i));
7593 } else {
7594 ans.emplace_back(min(abs(*l - i), abs(*g - i)));
7595 }
7596 }
7597 return ans;
7598 }
int vec[100010]
Definition: pat.cpp:5095

引用了 pat::a::a7_2::vec.

被这些函数引用 leetcode::shortest_distance_to_target_color::TEST().


该类的文档由以下文件生成: