Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit399814b

Browse files
committed
linkedlist
1 parentf855c81 commit399814b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

‎dp/WordBreak2.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ public static void dfs3(String s, Set<String> dict,
289289
/*
290290
// 解法4:先用DP来求解某些字段是否能word break,然后再做
291291
*/
292-
// 我们用DFS来解决这个问题吧
293292
publicstaticList<String>wordBreak4(Strings,Set<String>dict) {
294293
if (s ==null ||s.length() ==0 ||dict ==null) {
295294
returnnull;

‎tree/Connect.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packageAlgorithms.tree;
2+
3+
importjava.util.LinkedList;
4+
importjava.util.Queue;
5+
6+
/**
7+
* Definition for binary tree with next pointer.
8+
* public class TreeLinkNode {
9+
* int val;
10+
* TreeLinkNode left, right, next;
11+
* TreeLinkNode(int x) { val = x; }
12+
* }
13+
*/
14+
publicclassConnect {
15+
publicvoidconnect(TreeLinkNoderoot) {
16+
if (root ==null) {
17+
return;
18+
}
19+
20+
TreeLinkNodedummy =newTreeLinkNode(0);
21+
Queue<TreeLinkNode>q =newLinkedList<TreeLinkNode>();
22+
q.offer(root);
23+
q.offer(dummy);
24+
25+
while (!q.isEmpty()) {
26+
TreeLinkNodecur =q.poll();
27+
if (cur ==dummy) {
28+
if (!q.isEmpty()) {
29+
q.offer(dummy);
30+
}
31+
continue;
32+
}
33+
34+
if (q.peek() ==dummy) {
35+
cur.next =null;
36+
}else {
37+
cur.next =q.peek();
38+
}
39+
40+
if (cur.left !=null) {
41+
q.offer(cur.left);
42+
}
43+
44+
if (cur.right !=null) {
45+
q.offer(cur.right);
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp