趋势科技8.5笔试

第一题,字符串替换,双指针法
class Solution {
private:
    
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param my_template string字符串 
     * @param keys string字符串vector 
     * @param values string字符串vector 
     * @return string字符串
     */
    string token_replace(string my_template, vector<string>& keys, vector<string>& values) {
        // write code here
        unordered_map<string, string> mp;
        for (int i = 0; i < keys.size(); ++i) {
            mp[keys[i]] = values[i];
        }
        string res;
        int l = 0;
        while (l < my_template.size()) {
            if (l < my_template.size() && my_template[l] != '%') {
                res.push_back(my_template[l]);
                l++;
                continue;
            } else {
                int r = l + 1;
                while (r < my_template.size() && my_template[r] != '%') {
                    ++r;
                }
                if (r == my_template.size()) {
                    res += my_template.substr(l);
                    return res;
                }
                string temp = my_template.substr(l + 1, r - l - 1);
                if (mp.find(temp) != mp.end()) {
                    res += mp[temp];
                    l = r + 1;
                    continue;
                } else {
                    res.push_back('%');
                }
                ++l;
            }
            
        }
        return res;
    }
};
第二题,类似leetcode岛屿数量,另外要求,同一岛屿内最大曼哈顿距离
dfs,暴力计算每个岛屿内的曼哈顿距离
class Solution {
public:
    vector<vector<int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    vector<vector<vector<int>>> vec;
    vector<vector<int>> path;
    void dfs(vector<vector<int> >& graph, int row, int col) {
        if (row < 0 || row >= graph.size() || col < 0 || col >= graph[0].size()) {
            return;
        }
        if (graph[row][col] == 0 || graph[row][col] == 2) {
            return;
        }
        path.push_back({row, col});
        graph[row][col] = 2;
        for (auto& d : dir) {
            dfs(graph, row + d[0], col + d[1]);
        }
    }
    
    int getMaxDis(vector<vector<int> >& nums) {
        int maxDis = 0;
        for (int i = 0; i < nums.size(); ++i){
            for (int j = i + 1; j < nums.size(); ++j) {
                int t = abs(nums[i][0] - nums[j][0]) + abs(nums[i][1] - nums[j][1]);
                maxDis = max(maxDis, t);
            }
        }
        return maxDis;
    }
    
    vector<int> solve(vector<vector<int> >& graph) {
        // write code here
        int cnt = 0;
        int maxDis = 0;
        for (int i = 0; i < graph.size(); ++i) {
            for (int j = 0; j < graph[0].size(); ++j) {
                if (graph[i][j] == 1) {
                    cnt++;
                    dfs(graph, i, j);
                    vec.push_back(path);
                    path.clear();
                }
            }
        }
        for (auto& v : vec) {
            maxDis = max(maxDis, getMaxDis(v));
        }
        return {cnt, maxDis};
    }
};



全部评论
全通过了吗
点赞 回复 分享
发布于 2022-08-06 07:55
第一题不用考虑%%的情况吗
点赞 回复 分享
发布于 2022-08-05 23:51

相关推荐

ResourceUt...:楼主有自己的垃圾箱,公司也有自己的人才库
点赞 评论 收藏
分享
评论
11
33
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务