Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

精选leecode300题,哈希表最后一题718 #136

Open
tjl8787 opened this issue Nov 15, 2023 · 1 comment
Open

精选leecode300题,哈希表最后一题718 #136

tjl8787 opened this issue Nov 15, 2023 · 1 comment

Comments

@tjl8787
Copy link

tjl8787 commented Nov 15, 2023

秀哥好,我来提交issue啦,第一次提有点不熟悉。
参考你的dp解法时
nums1 =[1,2,3,2,8]
nums2 =[5,6,1,4,7]
这个测试用例会过不了,我的改法是,在你代码对边界判定是否相等的if语句内也加入maxNum = max(maxNum, dp[i][j]);更新最长子树组长度,不过这样改完执行时间和内存都比较高。
if (i == 0 || j == 0) {
dp[i][j] = A[i] == B[j] ? 1 : 0;
maxNum = max(maxNum, dp[i][j]);
}
我的另一种改法是直接在第二重循环内写下:
if (nums1[i] == nums2[j]) {
        if (i == 0 || j == 0) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = dp[i-1][j-1] + 1;
        }
        maxNum = max(maxNum, dp[i][j]);
      }这样会更快一些

@1031507008
Copy link

1031507008 commented Nov 15, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants