Skip to content

Commit 91833a3

Browse files
committed
fix: java code
1 parent 4ddd157 commit 91833a3

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

solution/0200-0299/0214.Shortest Palindrome/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,9 @@ class Solution:
271271
```java
272272
class Solution {
273273
public String shortestPalindrome(String s) {
274-
char[] t = (s + "#" + new StringBuilder(s).reverse().toString() + "$").toCharArray();
275-
int n = t.length();
274+
String rev = new StringBuilder(s).reverse().toString();
275+
char[] t = (s + "#" + rev + "$").toCharArray();
276+
int n = t.length;
276277
int[] next = new int[n];
277278
next[0] = -1;
278279
for (int i = 2, j = 0; i < n;) {
@@ -284,7 +285,7 @@ class Solution {
284285
next[i++] = 0;
285286
}
286287
}
287-
return new StringBuilder(s.substring(next[n - 1])).reverse().substring(0, s.length() - next[n - 1]) + s;
288+
return rev.substring(0, s.length() - next[n - 1]) + s;
288289
}
289290
}
290291
```

solution/0200-0299/0214.Shortest Palindrome/README_EN.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ class Solution:
252252
```java
253253
class Solution {
254254
public String shortestPalindrome(String s) {
255-
char[] t = (s + "#" + new StringBuilder(s).reverse().toString() + "$").toCharArray();
256-
int n = t.length();
255+
String rev = new StringBuilder(s).reverse().toString();
256+
char[] t = (s + "#" + rev + "$").toCharArray();
257+
int n = t.length;
257258
int[] next = new int[n];
258259
next[0] = -1;
259260
for (int i = 2, j = 0; i < n;) {
@@ -265,7 +266,7 @@ class Solution {
265266
next[i++] = 0;
266267
}
267268
}
268-
return new StringBuilder(s.substring(next[n - 1])).reverse().substring(0, s.length() - next[n - 1]) + s;
269+
return rev.substring(0, s.length() - next[n - 1]) + s;
269270
}
270271
}
271272
```

solution/0200-0299/0214.Shortest Palindrome/Solution2.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class Solution {
22
public String shortestPalindrome(String s) {
3-
char[] t = (s + "#" + new StringBuilder(s).reverse().toString() + "$").toCharArray();
4-
int n = t.length();
3+
String rev = new StringBuilder(s).reverse().toString();
4+
char[] t = (s + "#" + rev + "$").toCharArray();
5+
int n = t.length;
56
int[] next = new int[n];
67
next[0] = -1;
78
for (int i = 2, j = 0; i < n;) {
@@ -13,6 +14,6 @@ public String shortestPalindrome(String s) {
1314
next[i++] = 0;
1415
}
1516
}
16-
return new StringBuilder(s.substring(next[n - 1])).reverse().substring(0, s.length() - next[n - 1]) + s;
17+
return rev.substring(0, s.length() - next[n - 1]) + s;
1718
}
1819
}

0 commit comments

Comments
 (0)