Skip to content

Commit 4129efa

Browse files
committed
modify code on class
1 parent 4704a53 commit 4129efa

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/topinterviewquestions/Problem_0131_PalindromePartitioning.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class Problem_0131_PalindromePartitioning {
88

99
public static List<List<String>> partition(String s) {
10+
// dp[L][R] -> 是不是回文
1011
boolean[][] dp = getdp(s.toCharArray());
1112
LinkedList<String> path = new LinkedList<>();
1213
List<List<String>> ans = new ArrayList<>();
@@ -34,11 +35,20 @@ public static boolean[][] getdp(char[] str) {
3435
return dp;
3536
}
3637

37-
public static void process(String s, int index, LinkedList<String> path, boolean[][] dp, List<List<String>> ans) {
38+
// s 字符串
39+
// s[0...index-1] 已经做过的决定,放入了path中
40+
// 在index开始做属于这个位置的决定,
41+
// index == s.len path之前做的决定(一种分割方法),放进总答案ans里
42+
public static void process(String s, int index, LinkedList<String> path,
43+
boolean[][] dp, List<List<String>> ans) {
3844
if (index == s.length()) {
3945
ans.add(copy(path));
4046
} else {
4147
for (int end = index; end < s.length(); end++) {
48+
// index..index
49+
// index..index+1
50+
// index..index+2
51+
// index..end
4252
if (dp[index][end]) {
4353
path.addLast(s.substring(index, end + 1));
4454
process(s, end + 1, path, dp, ans);

src/topinterviewquestions/Problem_0134_GasStation.java

+78
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,83 @@ public static int lastIndex(int index, int size) {
140140
public static int nextIndex(int index, int size) {
141141
return index == size - 1 ? 0 : (index + 1);
142142
}
143+
144+
145+
// 以下的code是返回所有点是不是良好出发点的结果数组
146+
// public static boolean[] stations(int[] dis, int[] oil) {
147+
// if (dis == null || oil == null || dis.length < 2
148+
// || dis.length != oil.length) {
149+
// return null;
150+
// }
151+
// int init = changeDisArrayGetInit(dis, oil);
152+
// return init == -1 ? new boolean[dis.length] : enlargeArea(dis, init);
153+
// }
154+
//
155+
// public static int changeDisArrayGetInit(int[] dis, int[] oil) {
156+
// int init = -1;
157+
// for (int i = 0; i < dis.length; i++) {
158+
// dis[i] = oil[i] - dis[i];
159+
// if (dis[i] >= 0) {
160+
// init = i;
161+
// }
162+
// }
163+
// return init;
164+
// }
165+
//
166+
// public static boolean[] enlargeArea(int[] dis, int init) {
167+
// boolean[] res = new boolean[dis.length];
168+
// int start = init;
169+
// int end = nextIndex(init, dis.length);
170+
// int need = 0;
171+
// int rest = 0;
172+
// do {
173+
// // 当前来到的start已经在连通区域中,可以确定后续的开始点一定无法转完一圈
174+
// if (start != init && start == lastIndex(end, dis.length)) {
175+
// break;
176+
// }
177+
// // 当前来到的start不在连通区域中,就扩充连通区域
178+
// if (dis[start] < need) { // 当前start无法接到连通区的头部
179+
// need -= dis[start];
180+
// } else { // 当前start可以接到连通区的头部,开始扩充连通区域的尾巴
181+
// rest += dis[start] - need;
182+
// need = 0;
183+
// while (rest >= 0 && end != start) {
184+
// rest += dis[end];
185+
// end = nextIndex(end, dis.length);
186+
// }
187+
// // 如果连通区域已经覆盖整个环,当前的start是良好出发点,进入2阶段
188+
// if (rest >= 0) {
189+
// res[start] = true;
190+
// connectGood(dis, lastIndex(start, dis.length), init, res);
191+
// break;
192+
// }
193+
// }
194+
// start = lastIndex(start, dis.length);
195+
// } while (start != init);
196+
// return res;
197+
// }
198+
//
199+
// // 已知start的next方向上有一个良好出发点
200+
// // start如果可以达到这个良好出发点,那么从start出发一定可以转一圈
201+
// public static void connectGood(int[] dis, int start, int init, boolean[] res) {
202+
// int need = 0;
203+
// while (start != init) {
204+
// if (dis[start] < need) {
205+
// need -= dis[start];
206+
// } else {
207+
// res[start] = true;
208+
// need = 0;
209+
// }
210+
// start = lastIndex(start, dis.length);
211+
// }
212+
// }
213+
//
214+
// public static int lastIndex(int index, int size) {
215+
// return index == 0 ? (size - 1) : index - 1;
216+
// }
217+
//
218+
// public static int nextIndex(int index, int size) {
219+
// return index == size - 1 ? 0 : (index + 1);
220+
// }
143221

144222
}

0 commit comments

Comments
 (0)