Skip to content

Commit 2f16ee9

Browse files
JavaScalaDeveloperJavaScalaDeveloper
authored andcommitted
solution:5~29
1 parent 3c069b6 commit 2f16ee9

File tree

15 files changed

+486
-33
lines changed

15 files changed

+486
-33
lines changed

change/tools/listnode/ListNodeUtils.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package change.tools.listnode;
22

33

4-
import change.datastructure.*;
5-
import java.util.*;
4+
import change.datastructure.ListNode;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
68

79
/**
810
* 链表工具类
@@ -71,4 +73,16 @@ public static List<Integer> getCircularList(ListNode head) {
7173
return list;
7274
}
7375

76+
private static ListNode reverse(ListNode listNode) {
77+
ListNode pre = null;
78+
ListNode dump = listNode;
79+
while (dump != null) {
80+
ListNode next = dump.next;
81+
// 每一轮翻转以后,把当前节点及前给pre,把后面的节点给dump
82+
dump.next = pre;
83+
pre = dump;
84+
dump = next;
85+
}
86+
return pre;
87+
}
7488
}

solution/0000-0099/0005.Longest Palindromic Substring/Solution.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
package com.solution._0005;
2-
import change.datastructure.*;
3-
import java.util.*;
2+
43
public class Solution {
5-
public String longestPalindrome(String s) {
4+
public static void main(String[] args) {
5+
String s = "babad";
6+
String res = longestPalindrome(s);
7+
// System.out.printf(res);
8+
String res2 = longestPalindrome2(s);
9+
System.out.printf(res2);
10+
}
11+
12+
public static String longestPalindrome2(String s) {
13+
int len = s.length();
14+
if (len == 1) return s;
15+
int left = 0, right = 1;
16+
int max = 0;
17+
for (int i = 1; i < len - 1; i++) {
18+
int l = i - 1, r = i + 1;
19+
while (s.charAt(l) == s.charAt(r) && l > 0 && r < len - 1) {
20+
l--;
21+
r++;
22+
}
23+
if (r - l > max) {
24+
max = r - l;
25+
left = l;
26+
right = r;
27+
}
28+
}
29+
30+
return s.substring(left, right - 1);
31+
32+
}
33+
34+
public static String longestPalindrome(String s) {
635
int n = s.length();
736
boolean[][] dp = new boolean[n][n];
837
int mx = 1, start = 0;

solution/0000-0099/0006.Zigzag Conversion/Solution.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
11
package com.solution._0006;
22
import change.datastructure.*;
3+
import change.tools.listnode.ArrayUtils;
4+
35
import java.util.*;
46
public class Solution {
7+
public static void main(String[] args) {
8+
String s = "ABCDEFGHIJKLMNOPQRSTU";
9+
Solution solution = new Solution();
10+
String result = solution.convert(s, 4);
11+
System.out.println(result);
12+
String result2 = convert3(s, 4);
13+
System.out.println(result2);
14+
}
15+
/*
16+
这个算法的时间复杂度是 O(n),其中 n 是字符串 s 的长度。该算法使用一个StringBuilder数组来保存每一行的字符,然后通过遍历输入字符串来
17+
填充字符,同时跟踪当前行和移动方向。
18+
*/
19+
public static String convert3(String s, int numRows) {
20+
if (numRows == 1) {
21+
return s;
22+
}
23+
StringBuilder[] rows = new StringBuilder[numRows];
24+
int curRow = 0;
25+
boolean goingDown = false;
26+
for (char c : s.toCharArray()) {
27+
if (rows[curRow] == null) {
28+
rows[curRow] = new StringBuilder();
29+
}
30+
rows[curRow].append(c);
31+
if (curRow == 0 || curRow == numRows - 1) {
32+
// 决定往下或者往上
33+
goingDown = !goingDown;
34+
}
35+
// 进入往下或者往上一行
36+
curRow += goingDown ? 1 : -1;
37+
}
38+
StringBuilder result = new StringBuilder();
39+
for (StringBuilder row : rows) {
40+
result.append(row != null ? row.toString() : "");
41+
}
42+
return result.toString();
43+
}
44+
45+
public String convert2(String s, int numRows) {
46+
if (numRows == 1) {
47+
return s;
48+
}
49+
StringBuilder[] g = new StringBuilder[numRows];
50+
Arrays.setAll(g, k -> new StringBuilder());
51+
int i = 0, k = -1;
52+
for (char c : s.toCharArray()) {
53+
g[i].append(c);
54+
if (i == 0 || i == numRows - 1) {
55+
k = -k;
56+
}
57+
i += k;
58+
}
59+
return String.join("", g);
60+
}
561
public String convert(String s, int numRows) {
662
if (numRows == 1) {
763
return s;

solution/0000-0099/0007.Reverse Integer/Solution.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
package com.solution._0007;
2-
import change.datastructure.*;
3-
import java.util.*;
2+
43
public class Solution {
4+
public static void main(String[] args) {
5+
int res = reverse2(123);
6+
System.out.println(res);
7+
}
8+
9+
public static int reverse2(int x) {
10+
int res = 0;
11+
while (x != 0) {
12+
//res=0,x=123
13+
//res=3,x=12
14+
//res=32,x=1
15+
res = res * 10 + x % 10;
16+
x /= 10;
17+
}
18+
return res;
19+
}
20+
521
public int reverse(int x) {
622
int ans = 0;
723
for (; x != 0; x /= 10) {

solution/0000-0099/0010.Regular Expression Matching/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ class Solution {
125125
if (i > 0 && (p.charAt(j - 2) == '.' || p.charAt(j - 2) == s.charAt(i - 1))) {
126126
f[i][j] |= f[i - 1][j];
127127
}
128-
} else if (i > 0
129-
&& (p.charAt(j - 1) == '.' || p.charAt(j - 1) == s.charAt(i - 1))) {
128+
} else if (i > 0 && (p.charAt(j - 1) == '.' || p.charAt(j - 1) == s.charAt(i - 1))) {
130129
f[i][j] = f[i - 1][j - 1];
131130
}
132131
}
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.solution._0010;
2-
import change.datastructure.*;
3-
import java.util.*;
2+
43
public class Solution {
5-
public boolean isMatch(String s, String p) {
4+
public static void main(String[] args) {
5+
String s = "aaa", p = "a*";
6+
boolean res = isMatch(s, p);
7+
System.out.println(res);
8+
boolean res2 = isMatch2(s, p);
9+
System.out.println(res2);
10+
}
11+
12+
public static boolean isMatch(String s, String p) {
613
int m = s.length(), n = p.length();
714
boolean[][] f = new boolean[m + 1][n + 1];
815
f[0][0] = true;
@@ -13,12 +20,28 @@ public boolean isMatch(String s, String p) {
1320
if (i > 0 && (p.charAt(j - 2) == '.' || p.charAt(j - 2) == s.charAt(i - 1))) {
1421
f[i][j] |= f[i - 1][j];
1522
}
16-
} else if (i > 0
17-
&& (p.charAt(j - 1) == '.' || p.charAt(j - 1) == s.charAt(i - 1))) {
23+
} else if (i > 0 && (p.charAt(j - 1) == '.' || p.charAt(j - 1) == s.charAt(i - 1))) {
1824
f[i][j] = f[i - 1][j - 1];
1925
}
26+
// System.out.println("i=" + i + ",j=" + j + ",f[i][j]=" + f[i][j]);
2027
}
2128
}
2229
return f[m][n];
2330
}
31+
32+
/*
33+
递归
34+
*/
35+
public static boolean isMatch2(String s, String p) {
36+
if (p.isEmpty()) {
37+
return s.isEmpty();
38+
}
39+
boolean firstMatch = !s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.');
40+
if (p.length() >= 2 && p.charAt(1) == '*') {
41+
return isMatch2(s, p.substring(2)) || (firstMatch && isMatch2(s.substring(1), p));
42+
} else {
43+
return firstMatch && isMatch2(s.substring(1), p.substring(1));
44+
}
45+
}
46+
2447
}

solution/0000-0099/0011.Container With Most Water/Solution.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
import change.datastructure.*;
33
import java.util.*;
44
public class Solution {
5+
public static void main(String[] args) {
6+
7+
}
8+
public static int maxArea2(int[] height) {
9+
int l=0,r=height.length-1;
10+
int max=0;
11+
while (l < r) {
12+
max = Math.max(Math.min(height[l], height[r]) * (r - l), max);
13+
l++;
14+
r--;
15+
}
16+
return max;
17+
}
518
public int maxArea(int[] height) {
619
int i = 0, j = height.length - 1;
720
int ans = 0;

solution/0000-0099/0015.3Sum/Solution.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
import java.util.List;
66

77
public class Solution {
8+
public static void main(String[] args) {
9+
List<List<Integer>> res = threeSum(new int[]{-1, 0, 1, 2, -1, -4});
10+
System.out.println(res);
11+
}
812
/*
913
将数组排序,这样相同的数字就会挨在一起,方便进行去重;
1014
从左到右枚举数组中的每个数字作为第一个数 a;
1115
在 a 右边的区域内使用双指针 left 和 right,找出所有满足 a + nums[left] + nums[right] == 0 的三元组,并将其加入结果集;
1216
注意去重。
1317
*/
14-
public List<List<Integer>> threeSum(int[] nums) {
18+
public static List<List<Integer>> threeSum(int[] nums) {
1519
List<List<Integer>> result = new ArrayList<>();
1620
int n = nums.length;
1721
Arrays.sort(nums); // 排序

solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
11
package com.solution._0017;
2-
import change.datastructure.*;
3-
import java.util.*;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
46
public class Solution {
5-
public List<String> letterCombinations(String digits) {
7+
public static void main(String[] args) {
8+
List<String> res = letterCombinations("2345");
9+
System.out.println(res);
10+
List<String> res2 = letterCombinations2("2345");
11+
System.out.println(res2);
12+
}
13+
14+
/*
15+
递归调用 dfs 函数来遍历所有可能的情况。对于每一层递归,我们都会将当前数字对应的所有字母添加到当前组合中,然后继续搜索下一层。
16+
当到达最后一个数字时,我们将当前组合添加到结果列表中并返回上一层递归。
17+
该算法的时间复杂度是 O(3^n * 4^m),其中 n 和 m 分别是数字 7 和 9 的数目。因为每个数字对应的字母数都不超过 4 个,且输入字符
18+
串中只包含数字 2 到 9,因此在最坏情况下,我们需要遍历的所有情况数是 3^n * 4^m 种,其中 n 和 m 分别是数字 7 和 9 的数目。
19+
*/
20+
private static final String[] mapping = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
21+
private static final List<String> result = new ArrayList<>();
22+
23+
public static List<String> letterCombinations2(String digits) {
24+
if (digits == null || digits.length() == 0) {
25+
return new ArrayList<>();
26+
}
27+
dfs(digits, "");
28+
return result;
29+
}
30+
31+
private static void dfs(String digits, String combination) {
32+
System.out.println("digits=" + digits + ",combination=" + combination + ",");
33+
if (digits.length() == 0) {
34+
result.add(combination);
35+
return;
36+
}
37+
String letters = mapping[digits.charAt(0) - '0'];
38+
for (int i = 0; i < letters.length(); i++) {
39+
dfs(digits.substring(1), combination + letters.charAt(i));
40+
}
41+
}
42+
43+
public static List<String> letterCombinations(String digits) {
644
List<String> ans = new ArrayList<>();
745
if (digits.length() == 0) {
846
return ans;
947
}
1048
ans.add("");
11-
String[] d = new String[] {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
49+
String[] d = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
1250
for (char i : digits.toCharArray()) {
1351
String s = d[i - '2'];
1452
List<String> t = new ArrayList<>();

solution/0000-0099/0022.Generate Parentheses/Solution.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ public class Solution {
55
private List<String> ans = new ArrayList<>();
66
private int n;
77

8+
public static void main(String[] args) {
9+
Solution solution = new Solution();
10+
List<String> res = solution.generateParenthesis(4);
11+
System.out.println(res);
12+
}
813
public List<String> generateParenthesis(int n) {
914
this.n = n;
1015
dfs(0, 0, "");

0 commit comments

Comments
 (0)