From c1497b6e6eb4e2aa0c79b088d376561b04d628cf Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Mon, 19 Sep 2022 21:02:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=201035.=E4=B8=8D?= =?UTF-8?q?=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF.md=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原代码缩进、命名、空格不规范 --- ...70\344\272\244\347\232\204\347\272\277.md" | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 831b60c863..391246756b 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -74,24 +74,27 @@ public: Java: - ```java +```java class Solution { - public int maxUncrossedLines(int[] A, int[] B) { - int [][] dp = new int[A.length+1][B.length+1]; - for(int i=1;i<=A.length;i++) { - for(int j=1;j<=B.length;j++) { - if (A[i-1]==B[j-1]) { - dp[i][j]=dp[i-1][j-1]+1; - } - else { - dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]); - } - } - } - return dp[A.length][B.length]; - } + public int maxUncrossedLines(int[] nums1, int[] nums2) { + int len1 = nums1.length; + int len2 = nums2.length; + int[][] dp = new int[len1 + 1][len2 + 1]; + + for (int i = 1; i <= len1; i++) { + for (int j = 1; j <= len2; j++) { + if (nums1[i - 1] == nums2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + + return dp[len1][len2]; + } } - ``` +``` Python: ```python From 00aed2a72b6713b937c0379988a7e97d8383e329 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Tue, 20 Sep 2022 20:46:20 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200583.=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C.md=20Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加另一种动态规划思路 --- ...40\351\231\244\346\223\215\344\275\234.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 36c175c363..fd80853e14 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -128,6 +128,28 @@ public: Java: ```java +// dp数组中存储word1和word2最长相同子序列的长度 +class Solution { + public int minDistance(String word1, String word2) { + int len1 = word1.length(); + int len2 = word2.length(); + int[][] dp = new int[len1 + 1][len2 + 1]; + + for (int i = 1; i <= len1; i++) { + for (int j = 1; j <= len2; j++) { + if (word1.charAt(i - 1) == word2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + + return len1 + len2 - dp[len1][len2] * 2; + } +} + +// dp数组中存储需要删除的字符个数 class Solution { public int minDistance(String word1, String word2) { int[][] dp = new int[word1.length() + 1][word2.length() + 1]; From 8fad92673f0cd593d483436cb718542e8fa60a3a Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Thu, 22 Sep 2022 22:23:24 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200496.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md=20Ja?= =?UTF-8?q?va=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加另一种思路的Java代码,更加清晰简洁 --- ...4\345\244\247\345\205\203\347\264\240I.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 3c94881542..31e6f230b7 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -221,6 +221,32 @@ class Solution { return res; } } + +// 版本2 +class Solution { + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + HashMap map = new HashMap<>(); + for (int i = 0; i < nums1.length; i++) { + map.put(nums1[i], i); + } + + int[] res = new int[nums1.length]; + Stack stack = new Stack<>(); + Arrays.fill(res, -1); + + for (int i = 0; i < nums2.length; i++) { + while (!stack.isEmpty() && nums2[stack.peek()] < nums2[i]) { + int pre = nums2[stack.pop()]; + if (map.containsKey(pre)) { + res[map.get(pre)] = nums2[i]; + } + } + stack.push(i); + } + + return res; + } +} ``` Python3: ```python