From c1497b6e6eb4e2aa0c79b088d376561b04d628cf Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: 2022年9月19日 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 原代码缩进、命名、空格不规范 --- ...70344円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: 2022年9月20日 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 添加另一种动态规划思路 --- ...40351円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: 2022年9月22日 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代码,更加清晰简洁 --- ...4345円244円247円345円205円203円347円264円240円I.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円240円I.md" "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.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円240円I.md" +++ "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.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

AltStyle によって変換されたページ (->オリジナル) /