From 738663b9c09b1bc3f588db451441722e544d6b78 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 9 Nov 2021 12:59:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8update:=20Modify=20488?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\274\210\345\233\260\351\232\276\357\274\211.md" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" "b/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" index 6b286127..bf021e99 100644 --- "a/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" +++ "b/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" @@ -82,7 +82,7 @@ Tag : 「DFS」、「搜索」、「启发式搜索」 但在爆搜过程中同时维持两个字符串构造会超时,考虑使用一个 `int` 来记录 $hand$ 的使用情况。 -![image.png](https://pic.leetcode-cn.com/1636422194-vvIBfl-image.png) +![image.png](https://pic.leetcode-cn.com/1636432789-oDiehx-image.png) 代码: ```Java @@ -106,6 +106,7 @@ class Solution { if (((cur >> i) & 1) == 1) continue; int next = (1 << i) | cur; for (int j = 0; j <= n; j++) { + if (j > 0 && j < n - 1 && a.charAt(j) == a.charAt(j - 1)) continue; StringBuilder sb = new StringBuilder(); sb.append(a.substring(0, j)).append(b.substring(i, i + 1)); if (j != n) sb.append(a.substring(j)); @@ -139,7 +140,7 @@ class Solution { 我们建立一个类 `Node` 来代指当前搜索局面。 -```Java +```Java [] class Node { // 当前的棋盘状况 String a; @@ -169,7 +170,7 @@ class Node { 需要注意的是:对于某个局面 $node$ 而言,最终的距离是由「已确定距离」+「估值距离」两部分组成,我们应当根据这两部分之和进行出队,才能确保算法的正确性。 -![image.png](https://pic.leetcode-cn.com/1636424014-cCaHWU-image.png) +![image.png](https://pic.leetcode-cn.com/1636432738-eYQwfD-image.png) 代码: ```Java @@ -206,9 +207,7 @@ class Solution { public int findMinStep(String _a, String _b) { b = _b; m = b.length(); - PriorityQueue q = new PriorityQueue<>((o1,o2)->{ - return (o1.val + o1.step) - (o2.val + o2.step); - }); + PriorityQueue q = new PriorityQueue<>((o1,o2)->(o1.val+o1.step)-(o2.val+o2.step)); q.add(new Node(_a, 1 << m, f(_a, 1 << m), 0)); map.put(_a, 0); while (!q.isEmpty()) { @@ -221,6 +220,7 @@ class Solution { if (((cur >> i) & 1) == 1) continue; int next = (1 << i) | cur; for (int j = 0; j <= n; j++) { + if (j > 0 && j < n - 1 && a.charAt(j) == a.charAt(j - 1)) continue; StringBuilder sb = new StringBuilder(); sb.append(a.substring(0, j)).append(b.substring(i, i + 1)); if (j != n) sb.append(a.substring(j)); From 50e95c884bcbe429fea5093e47e34329be9de7c5 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 9 Nov 2021 17:55:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8update:=20Modify=20488?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...274\210\345\233\260\351\232\276\357\274\211.md" | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git "a/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" "b/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" index bf021e99..a19a4cf9 100644 --- "a/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" +++ "b/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" @@ -82,7 +82,7 @@ Tag : 「DFS」、「搜索」、「启发式搜索」 但在爆搜过程中同时维持两个字符串构造会超时,考虑使用一个 `int` 来记录 $hand$ 的使用情况。 -![image.png](https://pic.leetcode-cn.com/1636432789-oDiehx-image.png) +![image.png](https://pic.leetcode-cn.com/1636436638-kqDwIl-image.png) 代码: ```Java @@ -106,7 +106,10 @@ class Solution { if (((cur >> i) & 1) == 1) continue; int next = (1 << i) | cur; for (int j = 0; j <= n; j++) { - if (j > 0 && j < n - 1 && a.charAt(j) == a.charAt(j - 1)) continue; + boolean ok = false; + if (j > 0 && j < n && a.charAt(j) == a.charAt(j - 1) && a.charAt(j - 1) != b.charAt(i)) ok = true; + if (j < n && a.charAt(j) == b.charAt(i)) ok = true; + if (!ok) continue; StringBuilder sb = new StringBuilder(); sb.append(a.substring(0, j)).append(b.substring(i, i + 1)); if (j != n) sb.append(a.substring(j)); @@ -170,7 +173,7 @@ class Node { 需要注意的是:对于某个局面 $node$ 而言,最终的距离是由「已确定距离」+「估值距离」两部分组成,我们应当根据这两部分之和进行出队,才能确保算法的正确性。 -![image.png](https://pic.leetcode-cn.com/1636432738-eYQwfD-image.png) +![image.png](https://pic.leetcode-cn.com/1636436200-gPQhnD-image.png) 代码: ```Java @@ -220,7 +223,10 @@ class Solution { if (((cur >> i) & 1) == 1) continue; int next = (1 << i) | cur; for (int j = 0; j <= n; j++) { - if (j > 0 && j < n - 1 && a.charAt(j) == a.charAt(j - 1)) continue; + boolean ok = false; + if (j > 0 && j < n && a.charAt(j) == a.charAt(j - 1) && a.charAt(j - 1) != b.charAt(i)) ok = true; + if (j < n && a.charAt(j) == b.charAt(i)) ok = true; + if (!ok) continue; StringBuilder sb = new StringBuilder(); sb.append(a.substring(0, j)).append(b.substring(i, i + 1)); if (j != n) sb.append(a.substring(j));