Leetcode練習(6.17~6.23)

6.17-6.23
Tree專題
MEDIUM
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
https://leetcode.com/problems/path-sum/
https://leetcode.com/problems/path-sum-ii/
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
HARD
https://leetcode.com/problems/recover-binary-search-tree/

動態規劃專題
EASY
https://leetcode.com/problems/climbing-stairs/
MEDIUM
https://leetcode.com/problems/unique-paths/
https://leetcode.com/problems/longest-palindromic-substring/

二進制專題
MEDIUM
www.lintcode.com/zh-cn/problem/count-1-in-binary
www.lintcode.com/zh-cn/problem/add-binary

1 No.70 Climbing Staris

  • 動態規劃
public int climbStairs(int n) {
        if (n == 1) {
            return 1;
        }

        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;

        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }

        return dp[n];
    }

2 No.62 Unique Paths

  • 動態規劃
public static int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];
        for (int i = 0; i < n; i++) {
            dp[0][i] = 1;
        }
        for (int i = 0; i < m; i++) {
            dp[i][0] = 1;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }

        return dp[m-1][n-1];
    }

3 No.5 Longest Palindromic Substring

  • 動態規劃
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章