modify code

master
algorithmzuo 2 years ago
parent 0d8a65b963
commit 3b75509cf5

@ -1,6 +1,7 @@
package class17;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -27,8 +28,8 @@ public class Code03_PalindromePairs1 {
String reverse = reverse(word);
Integer rest = words.get("");
if (rest != null && rest != index && word.equals(reverse)) {
addRecord(res, rest, index);
addRecord(res, index, rest);
res.add(Arrays.asList(rest, index));
res.add(Arrays.asList(index, rest));
}
int[] rs = manacherss(word);
int mid = rs.length >> 1;
@ -36,7 +37,7 @@ public class Code03_PalindromePairs1 {
if (i - rs[i] == -1) {
rest = words.get(reverse.substring(0, mid - i));
if (rest != null && rest != index) {
addRecord(res, rest, index);
res.add(Arrays.asList(rest, index));
}
}
}
@ -44,20 +45,13 @@ public class Code03_PalindromePairs1 {
if (i + rs[i] == rs.length) {
rest = words.get(reverse.substring((mid << 1) - i));
if (rest != null && rest != index) {
addRecord(res, index, rest);
res.add(Arrays.asList(index, rest));
}
}
}
return res;
}
public static void addRecord(List<List<Integer>> res, int left, int right) {
List<Integer> newr = new ArrayList<>();
newr.add(left);
newr.add(right);
res.add(newr);
}
public static int[] manacherss(String word) {
char[] mchs = manachercs(word);
int[] rs = new int[mchs.length];

@ -1,6 +1,7 @@
package class17;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -77,8 +78,8 @@ public class Code03_PalindromePairs2 {
// 0代表""字符串
Integer rest = hash.get(0L);
if (rest != null && rest != index && word.equals(reverse)) {
addRecord(res, rest, index);
addRecord(res, index, rest);
res.add(Arrays.asList(rest, index));
res.add(Arrays.asList(index, rest));
}
if (!word.equals("")) {
buildHash(reverse);
@ -88,7 +89,7 @@ public class Code03_PalindromePairs2 {
if (i - rs[i] == -1) {
rest = hash.get(hashValue(0, mid - i - 1));
if (rest != null && rest != index) {
addRecord(res, rest, index);
res.add(Arrays.asList(rest, index));
}
}
}
@ -96,7 +97,7 @@ public class Code03_PalindromePairs2 {
if (i + rs[i] == rs.length) {
rest = hash.get(hashValue((mid << 1) - i, reverse.length() - 1));
if (rest != null && rest != index) {
addRecord(res, index, rest);
res.add(Arrays.asList(index, rest));
}
}
}
@ -104,13 +105,6 @@ public class Code03_PalindromePairs2 {
return res;
}
public static void addRecord(List<List<Integer>> res, int left, int right) {
List<Integer> newr = new ArrayList<>();
newr.add(left);
newr.add(right);
res.add(newr);
}
public static int[] manacherss(String word) {
char[] mchs = manachercs(word);
int[] rs = new int[mchs.length];

Loading…
Cancel
Save