Cadenza5566

2026/03/19

[LeetCode] 70 Climbing Stairs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
  static constexpr int SIZE = 46;
  int cache[SIZE];
  int dp(int x) {
    if (cache[x])
      return cache[x];
    if (x <= 1) {
      cache[x] = dp(x - 1);
      return cache[x];
    }
    cache[x] = dp(x - 1) + dp(x - 2);
    return cache[x];
  }

  int climbStairs(int n) {
    std::fill(std::begin(cache), std::end(cache), 0);
    cache[0] = 1;
    return dp(n);
  }
};
Labels: LeetCode
較新的文章 較舊的文章 首頁

網誌存檔

  • ▼  2026 (98)
    • ►  6月 (2)
    • ►  5月 (34)
    • ►  4月 (30)
    • ▼  3月 (23)
      • [LeetCode] 1143 Longest Common Subsequence
      • [LeetCode] 98 Validate Binary Search Tree
      • [LeetCode] 39 Combination Sum
      • [LeetCode] 572 Subtree of Another Tree
      • [LeetCode] 338 Counting Bits
      • [LeetCode] 242 Valid Anagram
      • [LeetCode] 226 Invert Binary Tree
      • [LeetCode] 217 Contains Duplicate
      • [LeetCode] 125 Valid Palindrome
      • [LeetCode] 121 Best Time to Buy and Sell Stock
      • [LeetCode] 104 Maximum Depth of Binary Tree
      • [LeetCode] 100 Same Tree
      • [LeetCode] 191 Number of 1 Bits
      • [LeetCode] 70 Climbing Stairs
      • [LeetCode] 190 Reverse Bits
      • [LeetCode ] 20 Valid Parentheses
      • [LeetCode] 268 Missing Number
      • [LeetCode] 23 Merge k Sorted Lists
      • [Leetcode] 15 3Sum
      • [LeetCode] 54 Spiral Matrix
      • [LeetCode] 300 Longest Increasing Subsequence
      • [LeetCode] 322 Coin Change
      • [LeetCode] 3 Longest Substring Without Repeating C...
    • ►  2月 (9)
頂尖企業主題. 技術提供:Blogger.