从零开始的刷LeetCode生活 第52期 576-600

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    vector<vector<vector<int>>>f;
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    int mod = 1e9 + 7;
    int findPaths(int m, int n, int N, int i, int j) {
        f = vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(N + 1, -1)));
        return dp(m, n, N, i, j);
    }

    int dp(int m, int n, int k, int x, int y)
    {
        int &v = f[x][y][k]; //在x,y这个点,可以走k步,方案数是多少
        if(v != -1) return v;
        v = 0;
        if(!k) return v;
        for(int i = 0; i < 4; i ++)
        {
            int a = x + dx[i], b = y + dy[i];
            if(a < 0 || a == m || b < 0 || b == n) v ++;//出界
            else v += dp(m, n, k - 1, a, b);
            v %= mod;
        }
        return v;
    }
};

在这里插入图片描述

class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {
        vector<int>tmp = nums;
        sort(tmp.begin(), tmp.end());
        int l = nums.size(), r = 0;
        for(int i = 0; i < nums.size(); i ++)
        {
            if(nums[i] != tmp[i])
            {
                l = min(l, i);
                r = max(r, i);
            }
        }
        return r - l < 0 ? 0 : r - l + 1;
    }
};

在这里插入图片描述

//lcs
class Solution {
public:
    int minDistance(string word1, string word2) {
        int n = word1.size(), m = word2.size();
        vector<vector<int>>dp(n + 1, vector<int>(m + 1));
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= m; j ++)
            {
                if(word1[i - 1] == word2[j - 1])
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                else
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        return n + m - 2 * dp[n][m];
    }
};

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int>res;
    vector<int> preorder(Node* root) {
        dfs(root);
        return res;
    }

    void dfs(Node* root)
    {
        if(!root) return;
        res.push_back(root->val);
        for(auto child : root->children)
            dfs(child);
        return;
    }
};

在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int>res;
    stack<Node*>stk;
    vector<int> preorder(Node* root) {
        if(!root) return res;
        stk.push(root);
        while(!stk.empty())
        {
            Node* node = stk.top();
            stk.pop();
            res.push_back(node->val);
            for(int i = node->children.size() - 1; ~i; i--)
                stk.push(node->children[i]);
        }
        return res;
    }
};

在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int>res;
    vector<int> postorder(Node* root) {
        if(!root) return res;
        for(auto child : root->children)
            postorder(child);
        res.push_back(root->val);
        return res;
    }
};
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int>res;
    stack<Node*>stk;
    vector<int> postorder(Node* root) {
        if(!root) return res;
        stk.push(root);        
        while(!stk.empty())
        {
            auto node = stk.top();
            stk.pop();
            res.push_back(node->val);
            for(Node* child : node->children)
                stk.push(child);
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    string fractionAddition(string expression) {
        if(expression.empty()) return "";
        string exp = isSign(expression[0]) ? expression : '+' + expression;
        pair<int, int>res{0, 1};
        for(int i = 0; i < exp.size(); i ++)
        {
            if(isSign(exp[i]))
                res = add(res, parse(exp, i));
        }
        return to_string(res.first) + "/" + to_string(res.second);
    }
    bool isSign(char c)
    {
        return c == '+' || c == '-';
    }
    bool isDigit(char c)
    {
        return c >= '0' && c <= '9';
    }
    void simplify(int& n, int& m)
    {
        if(n == 0)
        {
            m = 1;
            return;
        }
        int t = __gcd(n, m);
        n /= t;
        m /= t;
    }
    pair<int, int>add(const pair<int, int>p1, const pair<int, int>p2)
    {
        int n1 = p1.first; //分子
        int d1 = p1.second; //分母
        int n2 = p2.first; //分子
        int d2 = p2.second; //分母
        int n = n1 * d2 + n2 * d1;
        int d = d1 * d2;
        simplify(n, d);
        if(n * d < 0)
        {
            n = -abs(n);
            d = abs(d);
        }
        return {n, d};
    }
    pair<int, int>parse(const string& exp, int l)
    {
        int sign = exp[l] == '+' ? 1 : -1;
        int i = l + 1;
        int n = 0; 
        while(i < exp.size() && exp[i] != '/')
        {
            n = n * 10 + exp[i] - '0';
            i ++;
        }
        i ++;
        int d = 0;
        while(i < exp.size() && isDigit(exp[i]))
        {
            d = d * 10 + exp[i] - '0';
            i ++;
        }
        simplify(n, d);
        return {sign * n, d};
    }
};

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    double dis(vector<int>& p1, vector<int>& p2)
    {
        return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
    }
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        vector<double>d;
        d.push_back(dis(p1, p2));
        d.push_back(dis(p2, p3));
        d.push_back(dis(p3, p4));
        d.push_back(dis(p4, p1));
        d.push_back(dis(p1, p3));
        d.push_back(dis(p2, p4));
        sort(d.begin(), d.end());
        if(d[0] == 0)
            return false;
        else if(d[0] == d[1] && d[1] == d[2] && d[2] == d[3] && d[4] == d[5])
            return true;
        return false;
    }
};

在这里插入图片描述

class Solution {
public:
    int findLHS(vector<int>& nums) {
        unordered_map<int, int>hash;
        for(auto x : nums) hash[x] ++;
        int res = 0;
        for(auto t : hash)
        {
            if(hash.count(t.first + 1))
                res = max(res, t.second + hash[t.first + 1]);
        }
        return res;
    }
};
class Solution {
public:
    int findLHS(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int begin = 0, res = 0;
        for(int end = 0; end < nums.size(); end++)
        {
            while(nums[end] - nums[begin] > 1)
                begin ++;
            if(nums[end] - nums[begin] == 1)
                res = max(res, end - begin + 1);
        }
        return res;
    }
};

在这里插入图片描述
在这里插入图片描述

# Write your MySQL query statement below
select name, population, area 
from World 
where area > 3000000 or population > 25000000;

在这里插入图片描述
在这里插入图片描述

# Write your MySQL query statement below
select class
    from courses
    group by class
    having count(distinct student) >= 5;

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    int maxCount(int m, int n, vector<vector<int>>& ops) {
        int x_min = m, y_min = n;
        for(int i = 0; i < ops.size(); i ++)
        {
            x_min = min(x_min, ops[i][0]);
            y_min = min(y_min, ops[i][1]);
        }
        return x_min * y_min;
    }
};

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
        vector<string>res;
        unordered_map<string, int>hash;        
        for(int i = 0; i < list1.size(); i ++)
            hash[list1[i]] = i;
        // for(auto x : hash)
        //     cout << x.first << ' ' << x.second << endl;
        int minIndex = list1.size() + list2.size() - 2;
        for(int i = 0; i < list2.size(); i ++)
        {
            if(hash.count(list2[i]))
            {
                if(hash[list2[i]] + i == minIndex)
                    res.push_back(list2[i]);
                else if(hash[list2[i]] + i < minIndex)
                {
                    res.clear();
                    res.push_back(list2[i]);
                    minIndex = hash[list2[i]] + i;
                }
            }
        }
        return res;        
    }
};

在这里插入图片描述

class Solution {
public:
    int findIntegers(int num) {
        vector<int> f(32);
        f[0] = 1;
        f[1] = 2;
        for(int i = 2; i < f.size(); i ++)
            f[i] = f[i - 1] + f[i - 2];
        int i = 30, sum = 0, prev_bit = 0;
        while(i >= 0)
        {
            if((num & (1 << i)) != 0)
            {
                sum += f[i];
                if(prev_bit == 1)
                {
                    sum --;
                    break;
                }
                prev_bit = 1;
            }else
                prev_bit = 0;
            i --;            
        }
        return sum + 1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值