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

Commit13cb0fd

Browse files
refactor 725
1 parent2b6d01b commit13cb0fd

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

‎src/main/java/com/fishercoder/solutions/_725.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ public static class Solution1 {
77
/**
88
* My very original solution, but verbose.
99
*/
10-
publicListNode[]splitListToParts(ListNoderoot,intk) {
11-
intlen =getLength(root);
10+
publicListNode[]splitListToParts(ListNodehead,intk) {
11+
intlen =getLength(head);
1212
intaveSize =len /k;
1313
intextra =len %k;
1414
ListNode[]result =newListNode[k];
1515
for (inti =0;i <k;i++) {
16-
result[i] =root;
16+
result[i] =head;
1717
intaveSizeTmp =aveSize;
1818
aveSizeTmp +=extra-- >0 ?1 :0;
1919
intaveSizeTmp2 =aveSizeTmp;
2020
while (aveSizeTmp-- >0) {
21-
root =root.next;
21+
head =head.next;
2222
}
2323
if (result[i] !=null) {
2424
ListNodetmp =result[i];
@@ -46,17 +46,17 @@ public static class Solution2 {
4646
/**
4747
* More concise version
4848
*/
49-
publicListNode[]splitListToParts(ListNoderoot,intk) {
50-
intlen =getLength(root);
49+
publicListNode[]splitListToParts(ListNodehead,intk) {
50+
intlen =getLength(head);
5151
intaveSize =len /k;
5252
intextra =len %k;
5353
ListNode[]result =newListNode[k];
5454
ListNodeprev =null;
5555
for (inti =0;i <k;i++,extra--) {
56-
result[i] =root;
56+
result[i] =head;
5757
for (intj =0;j <aveSize + (extra >0 ?1 :0);j++) {
58-
prev =root;
59-
root =root.next;
58+
prev =head;
59+
head =head.next;
6060
}
6161
if (prev !=null) {
6262
prev.next =null;
@@ -75,4 +75,44 @@ private int getLength(ListNode root) {
7575
returnlen;
7676
}
7777
}
78+
79+
publicstaticclassSolution3 {
80+
/**
81+
* My original solution on 9/29/2021.
82+
*/
83+
publicListNode[]splitListToParts(ListNodehead,intk) {
84+
ListNode[]ans =newListNode[k];
85+
intsize =0;
86+
ListNodetmp =head;
87+
while (tmp !=null) {
88+
tmp =tmp.next;
89+
size++;
90+
}
91+
intminSize =size /k;
92+
intremainder =size %k;
93+
inti =0;
94+
if (head ==null) {
95+
while (i <k) {
96+
ans[i++] =null;
97+
}
98+
}
99+
while (i <k) {
100+
ListNodenode =head;
101+
tmp =node;
102+
intlen =minSize;
103+
if (remainder >0) {
104+
remainder--;
105+
len++;
106+
}
107+
while (len-- >1) {
108+
tmp =tmp.next;
109+
}
110+
head =tmp.next;
111+
tmp.next =null;
112+
ans[i] =node;
113+
i++;
114+
}
115+
returnans;
116+
}
117+
}
78118
}

‎src/test/java/com/fishercoder/_725Test.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
publicclass_725Test {
1010
privatestatic_725.Solution1solution1;
1111
privatestatic_725.Solution2solution2;
12+
privatestatic_725.Solution3solution3;
1213
privatestaticListNoderoot;
1314
privatestaticintk;
1415
privatestaticListNode[]actual;
@@ -17,6 +18,7 @@ public class _725Test {
1718
publicstaticvoidsetup() {
1819
solution1 =new_725.Solution1();
1920
solution2 =new_725.Solution2();
21+
solution3 =new_725.Solution3();
2022
}
2123

2224
@Test
@@ -59,4 +61,24 @@ public void test4() {
5961
}
6062
}
6163

64+
@Test
65+
publicvoidtest5() {
66+
root =LinkedListUtils.contructLinkedList(newint[]{1,2,3});
67+
k =5;
68+
actual =solution3.splitListToParts(root,k);
69+
for (ListNodehead :actual) {
70+
LinkedListUtils.printList(head);
71+
}
72+
}
73+
74+
@Test
75+
publicvoidtest6() {
76+
root =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4,5,6,7,8,9,10});
77+
k =3;
78+
actual =solution3.splitListToParts(root,k);
79+
for (ListNodehead :actual) {
80+
LinkedListUtils.printList(head);
81+
}
82+
}
83+
6284
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp