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

Commit664a91c

Browse files
committed
add two number
1 parent68c02a3 commit664a91c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

‎dp/WordBreak2.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,33 @@ public static boolean iswordBreak(String s, Set<String> dict) {
207207
returnD[len];
208208
}
209209

210+
/*
211+
// 解法3:重新剪枝。
212+
*/
213+
// 我们用DFS来解决这个问题吧
214+
publicstaticList<String>wordBreak3(Strings,Set<String>dict) {
215+
if (s ==null ||s.length() ==0 ||dict ==null) {
216+
returnnull;
217+
}
218+
219+
List<String>ret =newArrayList<String>();
220+
221+
// 记录切割过程中生成的字母
222+
List<String>path =newArrayList<String>();
223+
224+
intlen =s.length();
225+
226+
// 注意:一定要分配 Len+1 否则会爆哦.
227+
booleancanBreak[] =newboolean[len +1];
228+
for (inti =0;i <len +1;i++) {
229+
canBreak[i] =true;
230+
}
231+
232+
dfs3(s,dict,path,ret,0,canBreak);
233+
234+
returnret;
235+
}
236+
210237
// 我们用DFS模板来解决这个问题吧
211238
publicstaticvoiddfs3(Strings,Set<String>dict,
212239
List<String>path,List<String>ret,intindex,

‎list/AddTwoNumbers.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
packageAlgorithms.list;
2+
3+
importAlgorithms.algorithm.others.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
publicclassAddTwoNumbers {
17+
publicstaticvoidmain(String[]str) {
18+
ListNoden1 =newListNode(9);
19+
20+
ListNodel1 =newListNode(1);
21+
ListNodel2 =newListNode(9);
22+
ListNodel3 =newListNode(9);
23+
ListNodel4 =newListNode(9);
24+
ListNodel5 =newListNode(9);
25+
ListNodel6 =newListNode(9);
26+
ListNodel7 =newListNode(9);
27+
ListNodel8 =newListNode(9);
28+
ListNodel9 =newListNode(9);
29+
ListNodel10 =newListNode(9);
30+
31+
l1.next =l2;
32+
l2.next =l3;
33+
l3.next =l4;
34+
l4.next =l5;
35+
l5.next =l6;
36+
l6.next =l7;
37+
l7.next =l8;
38+
l8.next =l9;
39+
l9.next =l10;
40+
41+
System.out.println(addTwoNumbers(n1,l1).toString());
42+
}
43+
44+
publicstaticListNodeaddTwoNumbers(ListNodel1,ListNodel2) {
45+
if (l1 ==null ||l2 ==null) {
46+
returnnull;
47+
}
48+
49+
ListNodedummy =newListNode(0);
50+
ListNodetail =dummy;
51+
intcarry =0;
52+
53+
while (l1 !=null ||l2 !=null ||carry ==1) {
54+
intsum =carry;
55+
if (l1 !=null) {
56+
sum +=l1.val;
57+
l1 =l1.next;
58+
}
59+
60+
if (l2 !=null) {
61+
sum +=l2.val;
62+
l2 =l2.next;
63+
}
64+
65+
carry =sum /10;
66+
67+
// create a new node and add it to the tail.
68+
ListNodecur =newListNode(sum %10);
69+
tail.next =cur;
70+
tail =tail.next;
71+
}
72+
73+
returndummy.next;
74+
}
75+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp