Skip to content

Commit 2726063

Browse files
committed
fd
1 parent 9797686 commit 2726063

File tree

3 files changed

+89
-55
lines changed

3 files changed

+89
-55
lines changed

leetcode/solution/src/CopyListWithRandomPointer.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@ public class CopyListWithRandomPointer {
55

66

77
public RandomListNode copyRandomList(RandomListNode head) {
8-
for (RandomListNode node = head; node != null; ) {
9-
RandomListNode next = node.next;
10-
11-
RandomListNode copy = new RandomListNode(node.label);
12-
copy.next = next;
13-
node.next = copy;
14-
node = next;
8+
for (RandomListNode p = head; p != null; ) {
9+
RandomListNode next = p.next;
10+
RandomListNode temp = new RandomListNode(p.label);
11+
temp.next = p.next;
12+
p.next = temp;
13+
p = next;
1514
}
16-
17-
for (RandomListNode node = head; node != null; ) {
18-
node.next.random = node.random != null ? node.random.next : null;
19-
node = node.next.next;
15+
for (RandomListNode p = head; p != null; ) {
16+
if (p.random != null) {
17+
p.next.random = p.random.next;
18+
}
19+
p = p.next.next;
2020
}
21-
2221
RandomListNode dummy = new RandomListNode(0), cur = dummy;
23-
for (RandomListNode node = head; node != null; ) {
24-
cur.next = node.next;
22+
for (RandomListNode p = head; p != null; ) {
23+
cur.next = p.next;
2524
cur = cur.next;
26-
27-
node.next = node.next.next;
28-
node = node.next;
25+
p.next = p.next.next;
26+
p = p.next;
2927
}
30-
3128
return dummy.next;
3229
}
3330
}

leetcode/solution/src/LongestPalindromicSubstring.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ public class LongestPalindromicSubstring {
55

66
private int begin, maxLen;
77

8-
// 耗时14ms,平均复杂度O(n)
9-
public String longestPalindrome(String s) {
8+
// 耗时11ms,平均复杂度O(n)
9+
public String longestPalindrome(String s) {
10+
int[] res = new int[2];
1011
for (int i = 0; i < s.length(); i++) {
11-
helper(s, i, i);
12-
helper(s, i, i + 1);
12+
helper(s, i - 1, i + 1, res);
13+
helper(s, i, i + 1, res);
1314
}
14-
return s.substring(begin, begin + maxLen);
15+
return s.substring(res[1], res[1] + res[0]);
1516
}
1617

17-
private void helper(String s, int i, int j) {
18-
for (; i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j); i--, j++) ;
19-
int len = j - i - 1;
20-
if (len > maxLen) {
21-
maxLen = len;
22-
begin = i + 1;
18+
private void helper(String s, int left, int right, int[] res) {
19+
for ( ; left >= 0 && right < s.length(); left--, right++) {
20+
if (s.charAt(left) != s.charAt(right)) {
21+
break;
22+
}
23+
}
24+
int len = --right - ++left + 1;
25+
if (len > res[0]) {
26+
res[0] = len;
27+
res[1] = left;
2328
}
2429
}
2530

leetcode/src/Main.java

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,69 @@
22

33
public class Main {
44

5-
public boolean isValid(String s) {
6-
int[] stack = new int[s.length()];
7-
int index = 0;
5+
public String longestPalindrome(String s) {
6+
int[] res = new int[2];
87
for (int i = 0; i < s.length(); i++) {
9-
switch (s.charAt(i)) {
10-
case '(':
11-
case '[':
12-
case '{':
13-
stack[index++] = i;
14-
break;
15-
case ')':
16-
if (index == 0 || s.charAt(stack[--index]) != '(') {
17-
return false;
18-
}
19-
break;
20-
case ']':
21-
if (index == 0 || s.charAt(stack[--index]) != '[') {
22-
return false;
23-
}
24-
break;
25-
case '}':
26-
if (index == 0 || s.charAt(stack[--index]) != '{') {
27-
return false;
28-
}
29-
break;
8+
helper(s, i - 1, i + 1, res);
9+
helper(s, i, i + 1, res);
10+
}
11+
return s.substring(res[1], res[1] + res[0]);
12+
}
13+
14+
private void helper(String s, int left, int right, int[] res) {
15+
for ( ; left >= 0 && right < s.length(); left--, right++) {
16+
if (s.charAt(left) != s.charAt(right)) {
17+
break;
3018
}
3119
}
32-
return index == 0;
20+
int len = --right - ++left + 1;
21+
if (len > res[0]) {
22+
res[0] = len;
23+
res[1] = left;
24+
}
3325
}
3426

35-
public static void main(String[] args) {
27+
// public static String longestPalindrome(String s) {
28+
// if (s.length() == 0) {
29+
// return "";
30+
// }
31+
// int max = 0, idx = -1;
32+
// for (int i = 0; i < s.length(); i++) {
33+
// int len = findLen(s, i);
34+
// if (len > max) {
35+
// max = len;
36+
// idx = i;
37+
// }
38+
// }
39+
// if (max % 2 == 0) {
40+
// return s.substring(idx - max / 2 + 1, idx + max / 2 + 1);
41+
// } else {
42+
// return s.substring(idx - max / 2, idx + max / 2 + 1);
43+
// }
44+
// }
45+
//
46+
// private static int findLen(String s, int index) {
47+
// int len1 = 1;
48+
// for (int i = 1; i <= s.length(); i++) {
49+
// int i1 = index - i, i2 = index + i;
50+
// if (i1 < 0 || i2 >= s.length() || s.charAt(i1) != s.charAt(i2)) {
51+
// break;
52+
// }
53+
// len1 += 2;
54+
// }
55+
// int len2 = 0;
56+
// for (int i = 0; i < s.length(); i++) {
57+
// int i1 = index - i, i2 = index + i + 1;
58+
// if (i1 < 0 || i2 >= s.length() || s.charAt(i1) != s.charAt(i2)) {
59+
// break;
60+
// }
61+
// len2 += 2;
62+
// }
63+
// return Math.max(len1, len2);
64+
// }
3665

66+
public static void main(String[] args) {
67+
String s = longestPalindrome("cbbd");
68+
System.out.println(s);
3769
}
3870
}

0 commit comments

Comments
 (0)