从零开始的刷LeetCode生活 第39期 401-413

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

class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<string>res;
        for(int i = 0; i < 12; i ++)
            for(int j = 0; j < 60; j ++)
                if(count(i) + count(j) == num)
                    res.push_back(to_string(i) + ":" + 
                    (j < 10 ? "0" + to_string(j) : to_string(j)));
        return res;
    }
    int count(int n) //计算二进制中有多少个1
    {
        int res = 0;
        while(n != 0)
        {
            n = n & (n - 1);
            res ++;
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    string removeKdigits(string num, int k) {
        string res;
        for(auto c : num)
        {
            while(res.size() && res.back() > c && k)
            {
                res.pop_back();
                k--;
            }
            res += c;
        }
        while(k --) res.pop_back();
        int i = 0;
        while(i < res.size() && res[i] == '0') i ++;
        if(i == res.size()) return "0";
        return res.substr(i);
    }
};

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

class Solution {
public:
    bool canCross(vector<int>& stones) {
        map<int, set<int>>map;   
        for(int i = 0; i < stones.size(); i ++)
            map[stones[i]] = {};     
        map[0] = {0};                
        for(int i = 0; i < stones.size(); i ++)
            for(int x : map[stones[i]])
                for(int step = x - 1; step <= x + 1; step ++)
                    if(step > 0 && map.count(stones[i] + step))
                        map[stones[i] + step].insert(step);        
        return map[stones[stones.size() - 1]].size() > 0;
    }
};
class Solution {
public:
    bool canCross(vector<int>& stones) {
        int n = stones.size();
        vector<vector<bool>> dp(n, vector<bool>(n + 1, false));
        dp[0][0] = true;
        for(int i = 1; i < n; i ++)
            for(int j = 0; j < i; j ++)
            {
                int k = stones[i] - stones[j]; //间隔
                if(k < n + 1 && k <= stones[j] + 1)
                {
                    dp[i][k] = dp[j][k - 1] || dp[j][k] || dp[j][k + 1];
                    if(i == n - 1 && dp[i][k] == true) return true;
                }
            }
        return false;
    }
};

在这里插入图片描述

/**
 * 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 res = 0;
    int sumOfLeftLeaves(TreeNode* root) {
        dfs(root);
        return res;
    }
    void dfs(TreeNode* root)
    {
        if(!root) return;         
        dfs(root->left);
        dfs(root->right);
        if(root->left && !root->left->left && !root->left->right) res += root->left->val;
    }
};
/**
 * 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 sumOfLeftLeaves(TreeNode* root) {
        int cur = 0;
        if(!root) return 0;
        if(root->left && !root->left->left && !root->left->right)
            cur += root->left->val;
        return cur + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
    }    
};

在这里插入图片描述

class Solution {
public:
    string toHex(int num) {
        if(num == 0) return "0";
        string hex = "0123456789abcdef", res = "";
        while(num && res.size() < 8)
        {
            res = hex[num & 0xf] + res;
            num >>= 4;
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        auto cmp = [](const vector<int>&a, const vector<int>&b)
    {
        return a[0] > b[0] || a[0] == b[0] && a[1] < b[1];
    };
        sort(people.begin(), people.end(), cmp);
        vector<vector<int>> res;
        for(auto p : people) res.insert(res.begin() + p[1], p);
        return res;
    }
};

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

class Solution {
public:
    int trapRainWater(vector<vector<int>>& heightMap) {
        if(heightMap.empty()) return 0;
        int n = heightMap.size(), m = heightMap[0].size(), cur_max = INT_MIN, res = 0;
        if(n <= 2 || m <= 2) return 0;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>heap;//小根堆
        for(int i = 0; i < n; i ++) //左右边界
        {
            heap.push(make_pair(heightMap[i][0], i * m));//记录对应的高度和位置
            heap.push(make_pair(heightMap[i][m - 1], i * m + m - 1));
        }
        for(int j = 1; j < m - 1; j ++) //上下边界
        {
            heap.push(make_pair(heightMap[0][j], j));
            heap.push(make_pair(heightMap[n - 1][j], (n - 1) * m + j));
        }
        int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}, visit[n][m] = {};
        while(!heap.empty())
        {
            auto t = heap.top();
            heap.pop();
            cur_max = max(cur_max, t.first);
            int r = t.second / m, c = t.second % m;
            for(int i = 0; i < 4; i ++)
            {
                int x = r + dx[i], y = c + dy[i];
                if(x > 0 && x < n - 1 && y > 0 && y < m - 1 && !visit[x][y])
                {
                    visit[x][y] = 1;
                    if(heightMap[x][y] < cur_max)
                        res += cur_max - heightMap[x][y];
                    heap.push(make_pair(heightMap[x][y], x * m + y));
                }
            }
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int longestPalindrome(string s) {
        int cnts[60] = {0};
        for(int i = 0; i < s.size(); i ++)
            cnts[s[i] - 'A'] ++;
        int res = 0;
        for(auto cnt : cnts)
            res += cnt / 2 * 2;
        if(res < s.size())
            res ++;
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int splitArray(vector<int>& nums, int m) {
        int n = nums.size();
        vector<vector<long>> f(n + 1, vector<long>(m + 1, INT_MAX));//f[i][j]表示前i个分成j份的最大和
        vector<long> s(n + 1, 0); //前缀和
        f[0][0] = 0;
        for(int i = 0; i < n; i ++)
            s[i + 1] = s[i] + nums[i];
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= m; j ++)
                for(int k = 0; k < i; k ++)
                    f[i][j] = min(f[i][j], max(f[k][j - 1], s[i] - s[k]));  
        return f[n][m];
    }
};

在这里插入图片描述

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> res;
        for(int i = 1; i <= n; i ++)
        {            
            if(i % 3 == 0 && i % 5 == 0) res.push_back("FizzBuzz");
            else if(i % 3 == 0) res.push_back("Fizz");
            else if(i % 5 == 0) res.push_back("Buzz");            
            else res.push_back(to_string(i));
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& A) {
        if(A.size() < 3) return 0;
        int res = 0, dp = 0, d1 = A[1] - A[0];
        for(int i = 2; i < A.size(); i ++)
        {
            int d2 = A[i] - A[i - 1];
            if(d1 == d2)
                res += (++dp); //以A[i]为结尾的等差数列长度
            else dp = 0;
            d1 = d2;
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值