头像

kidforever911




离线:19小时前


最近来访(252)
用户头像
yxc的小迷妹
用户头像
xiongmao
用户头像
WA_1
用户头像
迎风飘扬
用户头像
锦木千束
用户头像
遇到坎就得迈过去
用户头像
拒绝emo
用户头像
violet_garden
用户头像
TonyJiang
用户头像
@Dong
用户头像
寂寞无语
用户头像
Mooncnc
用户头像
mzx
用户头像
KK_9
用户头像
ShenHanFeng
用户头像
Bug-Free
用户头像
wantongxue
用户头像
xy999
用户头像
转身ゝ未来
用户头像
aixc


kidforever911
20小时前
/**
 * 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:
    int maxAncestorDiff(TreeNode* root) {
        if(!root) return -1;
        int val = root->val;
        return max(dfs(root->left, val, val), dfs(root->right, val, val));
    }

    int dfs(TreeNode* root, int minv, int maxv) {
        if(!root) return -1;
        int val = root->val;
        //这里maxv和minv的语句位置不影响结果,因为第一次更新result与位置无关
        int result = max(abs(val - minv), abs(val - maxv));
        maxv = max(maxv, val);
        minv = min(minv, val);
        result = max(result, dfs(root->left, minv, maxv));
        result = max(result, dfs(root->right, minv, maxv));
        return result;
    }
};



kidforever911
20小时前
/**
 * 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:
    int maxAncestorDiff(TreeNode* root) {
        if(!root) return -1;
        int val = root->val;
        return max(dfs(root->left, val, val), dfs(root->right, val, val));
    }

    int dfs(TreeNode* root, int minv, int maxv) {
        if(!root) return -1;
        int val = root->val;
        //这里maxv和minv的语句位置不影响结果,因为第一次更新result与位置无关
        int result = max(abs(val - minv), abs(val - maxv));
        maxv = max(maxv, val);
        minv = min(minv, val);
        result = max(result, dfs(root->left, minv, maxv));
        result = max(result, dfs(root->right, minv, maxv));
        return result;
    }
};



#define x first
#define y second

class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
        if(mat.empty() || mat[0].empty()) return mat;
        int row = mat.size(), col = mat[0].size();
        vector<vector<int>> dist(row, vector<int>(col, -1));
        typedef pair<int, int> PII;
        queue<PII> q;

        for(int i = 0; i < row; i ++) {
            for(int j = 0; j < col; j ++) {
                if(mat[i][j] == 0) {
                    dist[i][j] = 0;
                    q.push({i, j});
                }
            }
        }
        int direction[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        while(q.size()) {
            PII cur = q.front();
            q.pop();
            for(int i = 0; i < 4; i ++) {
                int x = cur.x + direction[i][0];
                int y = cur.y + direction[i][1];
                if(x >= 0 && x < row && y >= 0 && y < col && dist[x][y] == -1) {
                    dist[x][y] = dist[cur.x][cur.y] + 1;
                    q.push({x, y});
                }
            }
        }
        return dist;
    }
};


活动打卡代码 LeetCode 542. 01 矩阵

#define x first
#define y second

class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
        if(mat.empty() || mat[0].empty()) return mat;
        int row = mat.size(), col = mat[0].size();
        vector<vector<int>> dist(row, vector<int>(col, -1));
        typedef pair<int, int> PII;
        queue<PII> q;

        for(int i = 0; i < row; i ++) {
            for(int j = 0; j < col; j ++) {
                if(mat[i][j] == 0) {
                    dist[i][j] = 0;
                    q.push({i, j});
                }
            }
        }
        int direction[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        while(q.size()) {
            PII cur = q.front();
            q.pop();
            for(int i = 0; i < 4; i ++) {
                int x = cur.x + direction[i][0];
                int y = cur.y + direction[i][1];
                if(x >= 0 && x < row && y >= 0 && y < col && dist[x][y] == -1) {
                    dist[x][y] = dist[cur.x][cur.y] + 1;
                    q.push({x, y});
                }
            }
        }
        return dist;
    }
};



class Solution {
public:
    string reverseStr(string s, int k) {
        int n = s.size();
        for(int i = 0; i < n; i += 2 * k) {
            reverse(s.begin() + i, s.begin() + min(n, i + k));
        }
        return s;
    }
};



class Solution {
public:
    string reverseStr(string s, int k) {
        int n = s.size();
        for(int i = 0; i < n; i += 2 * k) {
            reverse(s.begin() + i, s.begin() + min(n, i + k));
        }
        return s;
    }
};



class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        nums.push_back(nums.back() + 1);
        int left = 0, right = nums.size() / 2 - 1;
        while(left < right) {
            int mid = left + right >> 1;
            if(nums[mid * 2] != nums[mid * 2 + 1]) right = mid;
            else left = mid + 1;
        }
        return nums[right * 2];
    }
};



class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        nums.push_back(nums.back() + 1);
        int left = 0, right = nums.size() / 2 - 1;
        while(left < right) {
            int mid = left + right >> 1;
            if(nums[mid * 2] != nums[mid * 2 + 1]) right = mid;
            else left = mid + 1;
        }
        return nums[right * 2];
    }
};



class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        int result = INT_MAX;
        vector<int> record;
        for(auto& t : timePoints) {
            int h, m;
            sscanf(t.c_str(), "%d:%d", &h, &m);
            record.push_back(h * 60 + m);
        }
        sort(record.begin(), record.end());
        for(int i = 1; i < record.size(); i ++) result = min(result, record[i] - record[i - 1]);
        result = min(result, 24 * 60 - record.back() + record[0]);
        return result;
    }
};


活动打卡代码 LeetCode 539. 最小时间差

class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        int result = INT_MAX;
        vector<int> record;
        for(auto& t : timePoints) {
            int h, m;
            sscanf(t.c_str(), "%d:%d", &h, &m);
            record.push_back(h * 60 + m);
        }
        sort(record.begin(), record.end());
        for(int i = 1; i < record.size(); i ++) result = min(result, record[i] - record[i - 1]);
        result = min(result, 24 * 60 - record.back() + record[0]);
        return result;
    }
};