Cadenza5566

2026/03/30

[LeetCode] 1143 Longest Common Subsequence

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
  int longestCommonSubsequence(string s1, string s2) {
    constexpr int SIZE = 1001;
    int dp[SIZE][SIZE] = {0};

    for (int i = 1; i <= s1.length(); i++) {
      for (int j = 1; j <= s2.length(); j++) {
        if (s1[i - 1] == s2[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
    return dp[s1.length()][s2.length()];
  }
};
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.