【LeetCode 热题 100】打家劫舍 / 零钱兑换 / 单词拆分 / 乘积最大子数组 / 最长有效括号

⭐️个人主页:@小羊 ⭐️所属专栏:LeetCode 热题 100 很荣幸您能阅读我的文章,诚请评论指点,欢迎欢迎 ~

目录


爬楼梯

cpp 复制代码
class Solution {
public:
    int climbStairs(int n) {
        int a = 0, b = 0, c = 1;
        for (int i = 1; i <= n; i++)
        {
            a = b;
            b = c;
            c = a + b;
        }
        return c;
    }
};

杨辉三角

cpp 复制代码
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> res(numRows);
        for (int i = 0; i < numRows; i++)
        {
            res[i].resize(i + 1, 1);
            for (int j = 1; j < i; j++)
            {
                res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
            }
        }
        return res;
    }
};

打家劫舍

cpp 复制代码
class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n), g(n);
        f[0] = nums[0];
        for (int i = 1; i < n; i++)
        {
            f[i] = g[i - 1] + nums[i];
            g[i] = max(g[i - 1], f[i - 1]);
        }
        return max(f[n - 1], g[n - 1]);
    }
};

完全平方数

这是一个完全背包问题。

cpp 复制代码
class Solution {
public:
    int numSquares(int n) {
        int m = sqrt(n);
        vector<int> dp(n + 1, 0x3f3f3f3f);
        dp[0] = 0;
        for (int i = 1; i <= m; i++)
        {
            for (int j = i * i; j <= n; j++)
            {
                dp[j] = min(dp[j], dp[j - i * i] + 1);
            }
        }
        return dp[n];
    }
};

零钱兑换

cpp 复制代码
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        const int N = 0x3f3f3f3f;
        vector<int> dp(amount + 1, N);
        dp[0] = 0;
        for (int i = 0; i < coins.size(); i++)
        {
            for (int j = coins[i]; j <= amount; j++)
            {
                dp[j] = min(dp[j], dp[j - coins[i]] + 1);
            }
        }
        return dp[amount] >= N ? -1 : dp[amount];
    }
};

单词拆分

cpp 复制代码
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> hashset(wordDict.begin(), wordDict.end());
        int n = s.size();
        s = ' ' + s;
        vector<bool> dp(n + 1);
        dp[0] = true;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                if (dp[j - 1] && hashset.count(s.substr(j, i - j + 1)))
                {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
};

最长递增子序列

cpp 复制代码
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n, 1);
        int res = 1;
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (nums[j] < nums[i])
                    dp[i] = max(dp[i], dp[j] + 1);
                res = max(res, dp[i]);
            }
        }
        return res;
    }
};

贪心解法。

cpp 复制代码
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int n = nums.size();
        vector<int> v;
        for (auto e : nums)
        {
            if (v.empty() || e > v.back()) v.push_back(e);
            else
            {
                int l = 0, r = v.size() - 1;
                while (l < r)
                {
                    int mid = (r + l) / 2;
                    if (v[mid] < e) l = mid + 1;
                    else r = mid;
                }
                v[l] = e;
            }
        }
        return v.size();
    }
};

最大子数组和

dp[i] 表示以 i 位置为结尾的所有连续子数组的最大和。

cpp 复制代码
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n + 1);
        int res = -0x3f3f3f3f;
        for (int i = 1; i <= n; i++)
        {
            dp[i] = max(dp[i - 1], 0) + nums[i - 1];
            res = max(res, dp[i]);
        }
        return res;
    }
};
cpp 复制代码
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int pre = 0, res = -0x3f3f3f3f;
        for (auto e : nums)
        {
        	pre = (pre, 0) + e;
        	res = max(res, pre);
        }
        return res;
    }
};

乘积最大子数组

cpp 复制代码
class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size(), res = -0x3f3f3f3f;
        vector<int> f(n + 1, 1), g(n + 1, 1);
        for (int i = 1; i <= n; i++)
        {
            int x = nums[i - 1];
            int y = x * f[i - 1], z = x * g[i - 1];
            f[i] = max(x, max(y, z));
            g[i] = min(x, min(y, z));
            res = max(res, f[i]);
        }
        return res;
    }
};

分割等和子集

cpp 复制代码
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (auto e : nums) sum += e;
        if (sum % 2) return false;
        sum /= 2;
        vector<bool> dp(sum + 1);
        dp[0] = true;
        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = sum; j >= nums[i]; j--)
            {
                dp[j] = dp[j] || dp[j - nums[i]];
            }
        }
        return dp[sum];
    }
};

最长有效括号

初始化 -1 处理第一个字符就是 ) 的情况,避免栈操作错误。

栈中存储未匹配的 下标或无效 下标。

cpp 复制代码
class Solution {
public:
    int longestValidParentheses(string s) {
        stack<int> st;
        st.push(-1);
        int len = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '(') st.push(i);
            else
            {
                st.pop();
                if (st.empty()) st.push(i);
                else len = max(len, i - st.top());
            }
        }
        return len;
    }
};

本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~

相关推荐
剪一朵云爱着37 分钟前
力扣2438. 二的幂数组中查询范围内的乘积
算法·leetcode
测试老哥39 分钟前
Python+Selenium实现自动化测试
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
肥猪猪爸3 小时前
BP神经网络对时序数据进行分类
人工智能·深度学习·神经网络·算法·机器学习·分类·时序数据
dongzhenmao4 小时前
P1484 种树,特殊情形下的 WQS 二分转化。
数据结构·c++·windows·线性代数·算法·数学建模·动态规划
thusloop7 小时前
380. O(1) 时间插入、删除和获取随机元素
数据结构·算法·leetcode
MobotStone7 小时前
无代码+AI时代,为什么你仍然需要像个开发者一样思考
人工智能·算法
测试老哥7 小时前
软件测试之单元测试
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
緈福的街口7 小时前
【leetcode】584. 寻找用户推荐人
算法·leetcode·职场和发展
今天背单词了吗9807 小时前
算法学习笔记:17.蒙特卡洛算法 ——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·笔记·考研·算法·蒙特卡洛算法
Maybyy8 小时前
力扣242.有效的字母异位词
java·javascript·leetcode