从零开始的刷LeetCode生活 第47期 501-515

在这里插入图片描述

/**
 * 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:
    vector<int> res;
    vector<int> findMode(TreeNode* root) {
        if(!root) return res;
        TreeNode* pre = NULL; //前驱节点
        int curTimes = 1, maxTimes = 0;
        inOrder(root, pre, curTimes, maxTimes);
        return res;
    }

    void inOrder(TreeNode* root, TreeNode*&pre, int &curTimes, int &maxTimes) //pre要改变加&
    {
        if(!root) return;
        inOrder(root->left, pre, curTimes, maxTimes);
        if(pre)
            curTimes = (root->val == pre->val) ? curTimes + 1 : 1;
        if(curTimes == maxTimes)
            res.push_back(root->val);
        else if(curTimes > maxTimes)
        {
            res.clear();
            res.push_back(root->val);
            maxTimes = curTimes;
        }
        pre = root;
        inOrder(root->right, pre, curTimes, maxTimes);
    }
};

在这里插入图片描述

class Solution {
public:
    int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
        int n = Profits.size();
        vector<pair<int, int>> tmp(n);
        for(int i = 0; i < n; i ++)
            tmp[i] = make_pair(Capital[i], Profits[i]);

        //将项目按照资本从小到大排序。
        sort(tmp.begin(), tmp.end());
        priority_queue<int>heap; //存储最大利润的大根堆
        int i = 0;
        while(k --)
        {
            while(i < n && W >= tmp[i].first)
            {
                heap.push(tmp[i].second);
                i++;
            }
            if(!heap.empty())
            {
                W += heap.top();
                heap.pop();
            }
        }
        return W;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        vector<int>res(nums.size(), -1);
        vector<int>tmp(2 * nums.size(), -1);
        for(int i = 0; i < nums.size(); i ++)
        {
            tmp[i] = nums[i];
            tmp[nums.size() + i] = nums[i];
        }

        for(int i = 0; i < nums.size(); i ++)
            for(int j = i; j < tmp.size(); j ++)
                if(tmp[j] > nums[i])
                {
                    res[i] = tmp[j];
                    break;
                }                
        return res;
    }
};
class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n, -1);
        stack<int> stk;
        for(int i = 0; i < 2 * n; i ++)
        {
            while(!stk.empty() && nums[i % n] > nums[stk.top()])
                res[stk.top()] = nums[i % n], stk.pop();
            if(i < n) stk.push(i);               
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    string convertToBase7(int num) {
        if(num < 0) return string{'-'} + convertToBase7(-1 * num);
        if(num < 7) return to_string(num);
        return convertToBase7(num / 7) + to_string(num % 7);
    }
};
class Solution {
public:
    string convertToBase7(int num) {
        string res;
        if(num == 0) return "0";
        bool is_neg = false;
        if(num < 0) is_neg = true, num *= -1;
        while(num)
        {
            res += to_string(num % 7);
            num /= 7;
        }
        if(is_neg) res += '-';
        reverse(res.begin(), res.end());
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& nums) {
        int n = nums.size();
        vector<string>res(n);       
        map<int, int>m; //map自动排序       
        for(int i = 0; i < n; i ++)
            m[nums[i]] = i;
        for(auto x : m)
        {
            if(n == 1) res[x.second] = "Gold Medal";
            else if(n == 2) res[x.second] = "Silver Medal";
            else if(n == 3) res[x.second] = "Bronze Medal";
            else res[x.second] = to_string(n);
            n --;
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    bool checkPerfectNumber(int num) {
        if(num == 1) return false;
        int sum = 0;
        for(int i = 2; i * i < num; i ++)
            sum += num % i == 0 ? i + num / i : 0; //2 18 3 12 4 9 
        return sum + 1 == num;
    }
};

在这里插入图片描述

/**
 * 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:
    vector<int> findFrequentTreeSum(TreeNode* root) {
        int max_count = 0;
        unordered_map<int, int>m;
        dfs(root, m, max_count);
        vector<int>res;
        for(auto &p : m)
        {
            if(p.second == max_count)
                res.push_back(p.first);
        }
        return res;
    }
    int dfs(TreeNode* root, unordered_map<int, int>&m, int &max_count)
    {
        if(!root) return 0;
        int l = dfs(root->left, m, max_count);
        int r = dfs(root->right, m, max_count);
        int s = l + r + root->val;
        max_count = max(max_count, ++m[s]);
        return s;
    }
};

在这里插入图片描述

class Solution {
public:
    int fib(int N) {
        if(N == 0) return 0;
        if(N == 1) return 1;
        return fib(N - 1) + fib(N - 2);
    }
};
class Solution {
public:
    int fib(int N) {
    vector<int> dp(N + 3, 0);
    // base case
    dp[1] = dp[2] = 1;  //这里有bug 上面的初始化要大一点,不然数组越界
    for (int i = 3; i <= N; i++)
        dp[i] = dp[i - 1] + dp[i - 2];
    return dp[N];
    }
};
class Solution {
public:
    int fib(int N) {
        if(N == 0) return 0;
        if(N == 1 || N == 2) return 1;
        int prev = 1, curv = 1;
        for(int i = 3; i <= N; i ++)
        {
            int sum = prev + curv;
            prev = curv;
            curv = sum;
        }
        return curv;
    }
};

在这里插入图片描述

/**
 * 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 findBottomLeftValue(TreeNode* root) {
        if(!root) return -1;
        queue<TreeNode*>q;
        q.push(root);
        TreeNode*p;
        while(!q.empty())
        {
            p = q.front(), q.pop();
            if(p->right) q.push(p->right);
            if(p->left) q.push(p->left);
        }
        return p->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 res;
    int max = INT_MIN;
    int findBottomLeftValue(TreeNode* root) {
        dfs(root, 0);
        return res;
    }

    void dfs(TreeNode* root, int depth)
    {
        if(root)
        {
            if(!root->left && !root->right) //叶子节点
            {
                if(depth > max)
                {
                    max = depth;
                    res = root->val;
                }
            }
            dfs(root->left, depth + 1);
            dfs(root->right, depth + 1);
        }
    }
};

在这里插入图片描述

class Solution {
public:
    int findRotateSteps(string ring, string key) {
        int m = ring.size(), n = key.size(), cost = 0, res = INT_MAX;
        int dp[n][m];
        memset(dp, 0, n * m * sizeof(int)); //初始化
        for(int i = 0; i < m; i ++)
            if(ring[i] == key[0])
                dp[0][i] = min(i + 1, m - i + 1);
        for(int i = 1; i < n; i ++)
            for(int j = 0; j < m; j ++)
            {
                dp[i][j] = INT_MAX;
                if(key[i] == ring[j])
                {
                    for(int k = 0; k < m; k ++) //上一次十二点的位置
                    {
                        if(key[i - 1] == ring[k])
                        {
                            cost = dp[i - 1][k] + min((j - k + m) % m, (k - j + m) % m) + 1;
                            dp[i][j] = min(dp[i][j], cost);
                        }
                    }
                }
            }    
        for(int i = 0; i < m; i ++)
            if(ring[i] == key[n - 1])
                res = min(res, dp[n - 1][i]);
        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:
    vector<int>res;
    vector<int> largestValues(TreeNode* root) {
        if(!root) return res;
        dfs(root, 0);
        return res;
    }

    void dfs(TreeNode* root, int level)
    {
        if(!root) return;
        if(res.size() == level) res.push_back(root->val);
        res[level] = max(res[level], root->val);
        dfs(root->left, level + 1);
        dfs(root->right, level + 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:
    vector<int> largestValues(TreeNode* root) {
        vector<int> res;
        if(!root) return res;
        queue<TreeNode*>q;
        q.push(root);
        while(!q.empty())
        {
            int levelSize = q.size();
            int levelMax = INT_MIN; //当前行的最小值
            for(int i = 0; i < levelSize; i ++)
            {
                TreeNode* curNode = q.front();
                q.pop();
                levelMax = max(levelMax, curNode->val);
                if(curNode->left) q.push(curNode->left);
                if(curNode->right) q.push(curNode->right);
            }
            res.push_back(levelMax);
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值