从零开始的刷LeetCode生活 第22期 221-230

在这里插入图片描述

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if(matrix.empty()) return 0;
        int n = matrix.size(), m = matrix[0].size();
        vector<vector<int>> f(n + 1, vector<int>(m + 1, 0));
        int res = 0;
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= m; j ++)
                if(matrix[i - 1][j - 1] != '0')
                {
                    f[i][j] = min(f[i - 1][j], min(f[i][j - 1], f[i - 1][j - 1])) + 1;
                    res = max(res, f[i][j]);
                }
        return res * res;
    }
};

在这里插入图片描述

/**
 * 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:
    int countNodes(TreeNode* root) {
        if(!root) return 0;
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};
/**
 * 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:
    int countNodes(TreeNode* root) {
        if(!root) return 0;
        int l = 0, r = 0;
        TreeNode *leftP = root, *rightP = root;
        while(leftP)
        {
            l ++;
            leftP = leftP->left;
        }
        while(rightP)
        {
            r ++;
            rightP = rightP->right;
        }
        if(l == r) return (1 << l) - 1;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};

在这里插入图片描述

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        long long x = min(C, G) + 0ll - max(A, E);
        long long y = min(D, H) + 0ll - max(B, F);
        return (C - A) * (D - B) - max(0ll, x) * max(0ll, y) + (G - E) * (H - F);
    }
};

在这里插入图片描述

class Solution {
public:
    void calc(stack<char>&op, stack<int>&num) //把栈顶两个数拿出来操作
    {
        int x = num.top(); num.pop();
        int y = num.top(); num.pop();
        if(op.top() == '+') num.push(y + x);
        else num.push(y - x);
        op.pop();
    }
    int calculate(string s) {
        stack<char> op;
        stack<int> num;
        for(int i = 0; i < s.size(); i ++)
        {
            char c = s[i];
            if(c == ' ') continue;
            if(c == '(' || c == '+' || c == '-') op.push(c);
            else if(c == ')')
            {
                op.pop(); //弹出对应的左括号
                if(op.size() && op.top() != '(')
                    calc(op, num);
            }
            else
            {
                int j = i;
                while(j < s.size() && isdigit(s[j])) j ++;
                num.push(atoi(s.substr(i, j - i).c_str()));
                i = j - 1;
                if(op.size() && op.top() != '(') //不是左括号先计算两个
                    calc(op, num);
            }
        }
        return num.top();
    }
};

在这里插入图片描述

class MyStack {
public:
    queue<int> q;
    /** Initialize your data structure here. */
    MyStack() {
        
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        q.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int n = q.size();
        while(n -- > 1) q.push(q.front()), q.pop();
        int x = q.front();
        q.pop();
        return x;
    }
    
    /** Get the top element. */
    int top() {
        int n = q.size();
        while(n -- > 1) q.push(q.front()), q.pop();
        int x = q.front();
        q.pop(), q.push(x);
        return x;
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return q.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

在这里插入图片描述

/**
* 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* invertTree(TreeNode* root) {
       if(!root) return 0;
       swap(root->left ,root->right);
       invertTree(root->left);
       invertTree(root->right);
       return root;
   }
};

在这里插入图片描述

class Solution {
public:
    int calculate(string s) {
        if(s.size() == 0) return 0;
        int res = 0;
        stack<int> stk;
        int num = 0;
        char sign = '+'; 
        for(int i = 0; i <s.size(); i ++)
        {
            if(s[i] >= '0' && s[i] <= '9')
            {
                num *= 10;
                num = num - '0' + s[i];
            }
            if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || i == s.size() - 1)
            {
                if(sign == '+')
                    stk.push(num);
                if(sign == '-')
                    stk.push(-num);
                if(sign == '*')
                {
                    int top = stk.top();
                    stk.pop();
                    stk.push(num * top);
                }
                if(sign == '/')
                {
                    int top = stk.top();
                    stk.pop();
                    stk.push(top / num);
                }
                sign = s[i];
                num = 0;
            }
        }
        while(!stk.empty())
        {
            int top = stk.top();
            res += top;
            stk.pop();
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    string rangeToString(int st, int ed)
    {
        if(st == ed) return to_string(st);
        return to_string(st) + "->" + to_string(ed);
    }
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> res;
        if(nums.empty()) return res;
        int st = nums[0], ed = nums[0];
        for(int i = 1; i < nums.size(); i ++)
        {
            int x = nums[i];
            if(x > ed + 1)
            {
                res.push_back(rangeToString(st, ed));
                st = ed = x;
            }
            else ed ++;
        }
        res.push_back(rangeToString(st, ed));
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
        int n = nums.size();
        vector<int> res;
        if(!n) return res;
        int count1 = 0, count2 = 0, candidate1 = 0, candidate2 = 0;
        for(auto x : nums)
        {
            if(candidate1 == x) count1 ++;
            else if(candidate2 == x) count2 ++;
            else if(count1 == 0)
            {
                candidate1 = x;
                count1 = 1;
            }
            else if(count2 == 0)
            {
                candidate2 = x;
                count2 = 1;
            }
            else 
            {
                count1 --;
                count2 --;
            }
        }
        count1 = count2 = 0;
        for(auto x : nums)
        {
            if(candidate1 == x) count1 ++;
            else if(candidate2 == x) count2 ++;
        }
        if(count1 > n / 3) res.push_back(candidate1);
        if(count2 > n / 3) res.push_back(candidate2);
        return res;
    }
};

在这里插入图片描述

/**
 * 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:
    int kthSmallest(TreeNode* root, int k) {
        return dfs(root, k);
    }
    
    int dfs(TreeNode *root, int &k)
    {
        if(!root) return 0;
        int left = dfs(root->left, k);
        if(k <= 0) return left;
        if(--k == 0) return root->val;
        return dfs(root->right, k);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值