每日三题-爬楼梯、买卖股票的最佳时机、正则表达式匹配

简介: 每日三题-爬楼梯、买卖股票的最佳时机、正则表达式匹配

爬楼梯


39ba0286dbcb4e22bddc858874cb2797.png解法一

dp

class Solution {
    public int climbStairs(int n) {
        int dp[] = new int[n+1];
        if(n <= 2) return n;
        dp[1] = 1;
        dp[2] = 2;
        for(int i = 3;i <= n;i++){
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }
}

空间优化了一下

class Solution {
    public int climbStairs(int n) {
        int dp[] = new int[4];
        if(n <= 2) return n;
        dp[1] = 1;
        dp[2] = 2;
        for(int i = 3;i <= n;i++){
            dp[3] = dp[1] + dp[2];
            dp[1] = dp[2];
            dp[2] = dp[3];
        }
        return dp[3];
    }
}


买卖股票的最佳时机


f9e19f6bb6ec47d297457c2e497d4afd.png

class Solution {
    public int maxProfit(int[] prices) {
        int res = 0;
        int min = Integer.MAX_VALUE;
        for(int i = 0;i < prices.length;i++){
            res = Math.max(res,prices[i]-min);
            min = Math.min(min,prices[i]);
        }
        return res;
    }
}


正则表达式匹配


aa3c63a87b5e4cfe852eb10cd613d12c.png

class Solution {
    public boolean isMatch(String s, String p) {
        int m = s.length();
        int n = p.length();
        boolean[][] f = new boolean[m + 1][n + 1];
        f[0][0] = true;
        for (int i = 0; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (p.charAt(j - 1) == '*') {
                    f[i][j] = f[i][j - 2];
                    if (matches(s, p, i, j - 1)) {
                        f[i][j] = f[i][j] || f[i - 1][j];
                    }
                } else {
                    if (matches(s, p, i, j)) {
                        f[i][j] = f[i - 1][j - 1];
                    }
                }
            }
        }
        return f[m][n];
    }
    public boolean matches(String s, String p, int i, int j) {
        if (i == 0) {
            return false;
        }
        if (p.charAt(j - 1) == '.') {
            return true;
        }
        return s.charAt(i - 1) == p.charAt(j - 1);
    }
}
相关文章
|
3天前
|
算法 C++ Python
leetcode-122:买卖股票的最佳时机 II (贪心算法)
leetcode-122:买卖股票的最佳时机 II (贪心算法)
31 1
|
7月前
|
canal 算法
【Leetcode-121.买卖股票的最佳时机 -125.验证回文串】
【Leetcode-121.买卖股票的最佳时机 -125.验证回文串】
31 0
|
7月前
|
算法
代码随想录 Day27 贪心02上 LeetCode T122 买卖股票的最佳时机 II
代码随想录 Day27 贪心02上 LeetCode T122 买卖股票的最佳时机 II
24 0
|
3天前
leetcode代码记录(买卖股票的最佳时机 II
leetcode代码记录(买卖股票的最佳时机 II
9 1
|
3天前
|
算法
leetcode代码记录(买卖股票的最佳时机 III
leetcode代码记录(买卖股票的最佳时机 III
13 5
|
3天前
|
Python C++ 算法
C/C++每日一练(20230330) 闭区间、买卖股票最佳时机2、通配符匹配
C/C++每日一练(20230330) 闭区间、买卖股票最佳时机2、通配符匹配
57 0
C/C++每日一练(20230330) 闭区间、买卖股票最佳时机2、通配符匹配
|
3天前
代码随想录 Day41 动态规划09 LeetCode T121 买卖股票的最佳时机 T122 买卖股票的最佳时机II
代码随想录 Day41 动态规划09 LeetCode T121 买卖股票的最佳时机 T122 买卖股票的最佳时机II
27 0
|
3天前
代码随想录Day42 动态规划10 LeetCode T123 买卖股票的最佳时机III T188买卖股票的最佳时机IV
代码随想录Day42 动态规划10 LeetCode T123 买卖股票的最佳时机III T188买卖股票的最佳时机IV
34 0
|
11月前
|
算法
Leecode121. 买卖股票的最佳时机
Leecode121. 买卖股票的最佳时机
25 0
|
12月前
|
算法
一个函数解决【LeetCode 买卖股票的最佳时机】系列所有题目!
一个函数解决【LeetCode 买卖股票的最佳时机】系列所有题目!