Leetcode 第102场双周赛

Q1

循环

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
class Solution {
   public:
    vector<int> findColumnWidth(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        // int ans = 0;
        vector<int> ans(m, 0);
        for (int i = 0; i < m; i++) {
            int cnt = 0;
            for (int j = 0; j < n; j++) {
                // cout<<to_string(grid[j][i])<<" ";
                cnt = max(cnt, (int)to_string(grid[j][i]).size());
            }
            ans[i] = cnt;
        }
        return ans;
    }
};

Q2

预处理前缀

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;

class Solution {
   public:
    vector<long long> findPrefixScore(vector<int>& nums) {
        int n = nums.size();
        vector<long long> ans(n, 0), LMAX(n, 0);
        LMAX[0] = nums[0];
        for (int i = 1; i < n; i++) {
            LMAX[i] = max(LMAX[i - 1], 1ll * nums[i]);
        }
        vector<ll> conver(n, 0);
        for (int i = 0; i < n; i++) {
            conver[i] = nums[i] + LMAX[i];
        }
        ans[0]=conver[0];
        for (int i = 1; i < n; i++) {
            ans[i] = ans[i - 1] + conver[i];
        }
        return ans;
    }
};

Q3

二叉树的遍历
先把每一层的兄弟节点val之和处理出来。
那么对于每一个节点,新的val=这一层的val之和-自己的val-兄弟节点val。
每一次遍历把兄弟节点的val穿过去。
算是leetcode的特色题目。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
   public:
    map<int, int> mp;
    void pre(TreeNode* root, int depth) {
        if (root == nullptr) return;
        mp[depth] += root->val;
        pre(root->left, depth + 1);
        pre(root->right, depth + 1);
    }
    void dfs(TreeNode* root, int depth,int t) {
        if(root == nullptr) return;
        root->val = mp[depth]-root->val-t;
        int tl=root->left==nullptr?0:root->left->val;
        int tr=root->right==nullptr?0:root->right->val;
        dfs(root->left, depth + 1,tr);
        dfs(root->right, depth + 1,tl);
    }
    TreeNode* replaceValueInTree(TreeNode* root) {
        pre(root, 0);
        dfs(root, 0,0);
        return root;
    }
};

Q4

地杰斯特拉模板题
注意是单向边。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;

class Graph {
   public:
    vector<pair<int, int>> g[200];

    int Dijkstra(int st, int n, int ed) {
        vector<int> dis(n + 1, INT_MAX);
        dis[st] = 0;
        priority_queue<pair<int, int>, vector<pair<int, int>>,
                       greater<pair<int, int>>>
            q;
        q.push({0, st});
        while (!q.empty()) {
            auto t = q.top();
            q.pop();
            int u = t.second, d = t.first;
            if (d > dis[u]) continue;
            for (auto& e : g[u]) {
                int v = e.first, w = e.second;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    q.push({dis[v], v});
                }
            }
        }
        return dis[ed] == INT_MAX ? -1 : dis[ed];
    }

    Graph(int n, vector<vector<int>>& edges) {
        for (auto& arr : edges) {
            g[arr[0]].push_back({arr[1], arr[2]});
        }
    }

    void addEdge(vector<int> edge) {
        g[edge[0]].push_back({edge[1], edge[2]});
    }

    int shortestPath(int node1, int node2) {
        return Dijkstra(node1, 200, node2);
    }
};

/**
 * Your Graph object will be instantiated and called as such:
 * Graph* obj = new Graph(n, edges);
 * obj->addEdge(edge);
 * int param_2 = obj->shortestPath(node1,node2);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万伏小太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值