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

Commit46752ff

Browse files
add 1171
1 parente4b6553 commit46752ff

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ _If you like this project, please leave me a star._ ★
257257
|1180|[Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1180.java)||Easy|Math, String|
258258
|1176|[Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1176.java)||Easy|Array, Sliding Window|
259259
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java)||Easy|Math|
260+
|1171|[Remove Zero Sum Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1171.java)||Medium|LinkedList|
260261
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java)||Easy||
261262
|1161|[Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1161.java)||Medium|Graph|
262263
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)||Easy||
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.ListNode;
4+
5+
importjava.util.*;
6+
7+
publicclass_1171 {
8+
publicstaticclassSolution1 {
9+
/**
10+
* I keep shrinking the array whenever I found there's a range of sum that equals to zero
11+
* until the size of the list doesn't change any more.
12+
* This is probably not super efficient, but accepted on LeetCode.
13+
*/
14+
publicListNoderemoveZeroSumSublists(ListNodehead) {
15+
List<Integer>list =convertToList(head);
16+
intsize;
17+
do {
18+
size =list.size();
19+
list =shrinkList(list);
20+
}while (list.size() !=size);
21+
returnrecoverLinkedList(list);
22+
}
23+
24+
privateListNoderecoverLinkedList(List<Integer>list) {
25+
ListNodepre =newListNode(-1);
26+
ListNodetmp =pre;
27+
for (inti =0;i <list.size();i++) {
28+
tmp.next =newListNode(list.get(i));
29+
tmp =tmp.next;
30+
}
31+
returnpre.next;
32+
}
33+
34+
privateList<Integer>convertToList(ListNodehead) {
35+
List<Integer>list =newArrayList<>();
36+
while (head !=null) {
37+
if (head.val !=0) {
38+
//if it's zero, we'll just ignore it, this can help us take care of the zero values
39+
list.add(head.val);
40+
}
41+
head =head.next;
42+
}
43+
returnlist;
44+
}
45+
46+
privateList<Integer>shrinkList(List<Integer>list) {
47+
for (inti =0;i <list.size();i++) {
48+
intstart =i;
49+
List<Integer>preSumList =newArrayList<>();
50+
for (intk =0;k <start;k++) {
51+
preSumList.add(0);
52+
}
53+
preSumList.add(list.get(i));
54+
for (intk =i;k <list.size();k++) {
55+
if (k >start) {
56+
Integersum =preSumList.get(k -1) +list.get(k);
57+
if (sum ==0) {
58+
List<Integer>shrinkedList =newArrayList<>();
59+
for (intj =0;j <start;j++) {
60+
shrinkedList.add(list.get(j));
61+
}
62+
for (intj =k +1;j <list.size();j++) {
63+
shrinkedList.add(list.get(j));
64+
}
65+
returnshrinkedList;
66+
}else {
67+
preSumList.add(sum);
68+
}
69+
}
70+
}
71+
}
72+
returnlist;
73+
}
74+
}
75+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.LinkedListUtils;
4+
importcom.fishercoder.solutions._1171;
5+
importcom.fishercoder.solutions._96;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclass_1171Test {
12+
privatestatic_1171.Solution1solution1;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_1171.Solution1();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{3,1}),solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{1,2, -3,3,1})));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{1,2,4}),solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{1,2,3, -3,4})));
27+
}
28+
29+
@Test
30+
publicvoidtest3() {
31+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{1}),solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{1,2,3, -3, -2})));
32+
}
33+
34+
@Test
35+
publicvoidtest4() {
36+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{5, -2, -5}),
37+
solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{5, -3, -4,1,6, -2, -5})));
38+
}
39+
40+
@Test
41+
publicvoidtest5() {
42+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{}),
43+
solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{0})));
44+
}
45+
46+
@Test
47+
publicvoidtest6() {
48+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{2}),
49+
solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{2,0})));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp