头像

binacslee




离线:1小时前



binacslee
18天前
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root || root == p || root == q)
            return root;
        auto l = lowestCommonAncestor(root->left, p, q);
        auto r = lowestCommonAncestor(root->right, p, q);
        if (l && r)
            return root;
        return l ? l : r;
    }
};



binacslee
18天前
class Solution {
public:
    int strToInt(string str) {
        if (str.empty())
            return 0;
        int n = str.size();
        int p = 0, flag = 1;
        while (str[p] == ' ')
            p ++ ;
        if (str[p] == '-')
            p ++ , flag = -1;
        else if (str[p] == '+')
            p ++ ;

        int res = 0;
        while (str[p] >= '0' && str[p] <= '9') {
            int v = str[p] - '0';
            if (res > INT_MAX / 10 || res == INT_MAX / 10 && v > 7)
                return INT_MAX;
            else if (res < INT_MIN / 10 || res == INT_MIN / 10 && v > 8)
                return INT_MIN;
            res = res * 10 + flag * v;
            p ++ ;
        }
        return res;
    }
};


活动打卡代码 AcWing 86. 构建乘积数组

binacslee
18天前
class Solution {
public:
    vector<int> multiply(const vector<int>& A) {
        if (A.empty())
            return {};
        int n = A.size();
        vector<int> res(n);
        res[0] = 1;
        for (int i = 1, p = A[0]; i < n; ++ i )
            res[i] = p, p *= A[i];
        for (int i = n - 2, p = A[n - 1]; i >= 0; -- i )
            res[i] *= p, p *= A[i];
        return res;
    }
};



binacslee
18天前
class Solution {
public:
    int add(int num1, int num2){
        while (num2) {
            int sum = num1 ^ num2;
            int carry = (unsigned int)(num1 & num2) << 1;
            num1 = sum, num2 = carry;
        }
        return num1;
    }
};


活动打卡代码 AcWing 84. 求1+2+…+n

binacslee
18天前
class Solution {
public:
    int getSum(int n) {
        n && (n += getSum(n - 1));
        return n;
    }
};



binacslee
18天前
class Solution {
public:
    int maxDiff(vector<int>& nums) {
        int res = 0, minv = 1e9;
        for (auto v : nums)
            res = max(res, v - minv), minv = min(minv, v);
        return res;
    }
};



binacslee
18天前
class Solution {
public:
    int lastRemaining(int n, int m) {
        if (n == 1)
            return 0;
        return (lastRemaining(n - 1, m) + m)% n;
    }

    int lastRemaining2(int n, int m){
        int res = 0;
        for (int i = 2; i <= n; ++ i )
            res = (res + m) % i;
        return res;
    }
};


活动打卡代码 AcWing 81. 扑克牌的顺子

binacslee
18天前
class Solution {
public:
    bool isContinuous(vector<int> numbers ) {
        if (numbers.empty())
            return false;
        int minv = INT_MAX, maxv = INT_MIN;
        vector<bool> vis(15);
        for (auto v : numbers)
            if (v) {
                if (vis[v])
                    return false;
                else
                    vis[v] = true;
                minv = min(minv, v);
                maxv = max(maxv, v);
            }
        return (maxv - minv) < 5;
    }
};


活动打卡代码 AcWing 80. 骰子的点数

binacslee
18天前
class Solution {
public:
    vector<int> numberOfDice(int n) {
        vector<vector<int>> f(n + 1, vector<int>(6 * n + 1, 0));
        f[0][0] = 1;
        for (int i = 1; i <= n; ++ i )
            for (int j = 1; j <= i * 6; ++ j )
                for (int k = 1; k <= 6; ++ k )
                    if (j >= k)
                        f[i][j] += f[i - 1][j - k];
        return vector<int>(f[n].begin() + n, f[n].end());
    }
};




binacslee
18天前
class Solution {
public:
    vector<int> maxInWindows(vector<int>& nums, int k) {
        int n = nums.size();
        deque<int> q;
        vector<int> res;
        for (int i = 0; i < n; ++ i ) {
            if (!q.empty() && q.front() <= i - k)
                q.pop_front();
            while (!q.empty() && nums[q.back()] <= nums[i])
                q.pop_back();
            q.push_back(i);
            if (i >= k - 1)
                res.push_back(nums[q.front()]);
        }
        return res;
    }
};