Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 2e42fc9

Browse files
committed
1171 added
1 parent 93c9554 commit 2e42fc9

File tree

4 files changed

+132
-21
lines changed

4 files changed

+132
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [1171. Remove Zero Sum Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/)
2+
3+
Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.
4+
5+
After doing so, return the head of the final linked list. You may return any such answer.
6+
7+
(Note that in the examples below, all sequences are serializations of ListNode objects.)
8+
9+
Example 1:
10+
11+
```text
12+
Input: head = [1,2,-3,3,1]
13+
Output: [3,1]
14+
Note: The answer [1,2,1] would also be accepted.
15+
```
16+
17+
Example 2:
18+
19+
```text
20+
Input: head = [1,2,3,-3,4]
21+
Output: [1,2,4]
22+
```
23+
24+
Example 3:
25+
26+
```text
27+
Input: head = [1,2,3,-3,-2]
28+
Output: [1]
29+
```
30+
31+
Constraints:
32+
33+
- The given linked list will contain between 1 and 1000 nodes.
34+
- Each node in the linked list has -1000 <= node.val <= 1000.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package problem1171
2+
3+
import "github.com/aQuaYi/LeetCode-in-Go/kit"
4+
5+
// ListNode is ...
6+
type ListNode = kit.ListNode
7+
8+
func removeZeroSumSublists(head *ListNode) *ListNode {
9+
10+
return nil
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package problem1171
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aQuaYi/LeetCode-in-Go/kit"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
head []int
13+
ans []int
14+
}{
15+
16+
{
17+
[]int{1, 1, -3, 6, -3, 1},
18+
[]int{1, 1, 1},
19+
},
20+
21+
{
22+
[]int{1, 2, -2, 5, -3, 1},
23+
[]int{1, 5, -3, 1},
24+
},
25+
26+
{
27+
[]int{1, 2, -8, 3, 1},
28+
[]int{1, 2, -8, 3, 1},
29+
},
30+
31+
{
32+
[]int{1, 2, -3, 3, 1},
33+
[]int{3, 1},
34+
},
35+
36+
{
37+
[]int{1, 2, 3, -3, 4},
38+
[]int{1, 2, 4},
39+
},
40+
41+
{
42+
[]int{1, 2, 3, -3, -2},
43+
[]int{1},
44+
},
45+
46+
// 可以有多个 testcase
47+
}
48+
49+
func Test_removeZeroSumSublists(t *testing.T) {
50+
a := assert.New(t)
51+
52+
for _, tc := range tcs {
53+
head := kit.Ints2List(tc.head)
54+
ans := removeZeroSumSublists(head)
55+
a.Equal(tc.ans, ans, "输入:%v", tc)
56+
}
57+
}
58+
59+
func Benchmark_removeZeroSumSublists(b *testing.B) {
60+
for i := 0; i < b.N; i++ {
61+
for _, tc := range tcs {
62+
head := kit.Ints2List(tc.head)
63+
removeZeroSumSublists(head)
64+
}
65+
}
66+
}

leetcode.json

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 514,
4-
"Updated": "2019-08-27T17:22:53.716308063+08:00",
3+
"Ranking": 509,
4+
"Updated": "2019-08-28T14:04:14.716953462+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 258,
@@ -1621,7 +1621,7 @@
16211621
"ID": 133,
16221622
"Title": "Clone Graph",
16231623
"TitleSlug": "clone-graph",
1624-
"PassRate": "27%",
1624+
"PassRate": "28%",
16251625
"Difficulty": "Medium",
16261626
"IsAccepted": false,
16271627
"IsPaid": false,
@@ -3805,7 +3805,7 @@
38053805
"ID": 315,
38063806
"Title": "Count of Smaller Numbers After Self",
38073807
"TitleSlug": "count-of-smaller-numbers-after-self",
3808-
"PassRate": "38%",
3808+
"PassRate": "39%",
38093809
"Difficulty": "Hard",
38103810
"IsAccepted": true,
38113811
"IsPaid": false,
@@ -5365,7 +5365,7 @@
53655365
"ID": 445,
53665366
"Title": "Add Two Numbers II",
53675367
"TitleSlug": "add-two-numbers-ii",
5368-
"PassRate": "50%",
5368+
"PassRate": "51%",
53695369
"Difficulty": "Medium",
53705370
"IsAccepted": true,
53715371
"IsPaid": false,
@@ -8977,7 +8977,7 @@
89778977
"ID": 746,
89788978
"Title": "Min Cost Climbing Stairs",
89798979
"TitleSlug": "min-cost-climbing-stairs",
8980-
"PassRate": "47%",
8980+
"PassRate": "48%",
89818981
"Difficulty": "Easy",
89828982
"IsAccepted": true,
89838983
"IsPaid": false,
@@ -9601,7 +9601,7 @@
96019601
"ID": 798,
96029602
"Title": "Smallest Rotation with Highest Score",
96039603
"TitleSlug": "smallest-rotation-with-highest-score",
9604-
"PassRate": "40%",
9604+
"PassRate": "41%",
96059605
"Difficulty": "Hard",
96069606
"IsAccepted": true,
96079607
"IsPaid": false,
@@ -11293,7 +11293,7 @@
1129311293
"ID": 939,
1129411294
"Title": "Minimum Area Rectangle",
1129511295
"TitleSlug": "minimum-area-rectangle",
11296-
"PassRate": "50%",
11296+
"PassRate": "51%",
1129711297
"Difficulty": "Medium",
1129811298
"IsAccepted": true,
1129911299
"IsPaid": false,
@@ -11473,7 +11473,7 @@
1147311473
"ID": 954,
1147411474
"Title": "Array of Doubled Pairs",
1147511475
"TitleSlug": "array-of-doubled-pairs",
11476-
"PassRate": "34%",
11476+
"PassRate": "35%",
1147711477
"Difficulty": "Medium",
1147811478
"IsAccepted": true,
1147911479
"IsPaid": false,
@@ -12085,7 +12085,7 @@
1208512085
"ID": 1005,
1208612086
"Title": "Maximize Sum Of Array After K Negations",
1208712087
"TitleSlug": "maximize-sum-of-array-after-k-negations",
12088-
"PassRate": "49%",
12088+
"PassRate": "50%",
1208912089
"Difficulty": "Easy",
1209012090
"IsAccepted": true,
1209112091
"IsPaid": false,
@@ -12133,7 +12133,7 @@
1213312133
"ID": 1009,
1213412134
"Title": "Complement of Base 10 Integer",
1213512135
"TitleSlug": "complement-of-base-10-integer",
12136-
"PassRate": "59%",
12136+
"PassRate": "58%",
1213712137
"Difficulty": "Easy",
1213812138
"IsAccepted": true,
1213912139
"IsPaid": false,
@@ -12169,7 +12169,7 @@
1216912169
"ID": 1012,
1217012170
"Title": "Numbers With Repeated Digits",
1217112171
"TitleSlug": "numbers-with-repeated-digits",
12172-
"PassRate": "35%",
12172+
"PassRate": "34%",
1217312173
"Difficulty": "Hard",
1217412174
"IsAccepted": true,
1217512175
"IsPaid": false,
@@ -12217,7 +12217,7 @@
1221712217
"ID": 1016,
1221812218
"Title": "Binary String With Substrings Representing 1 To N",
1221912219
"TitleSlug": "binary-string-with-substrings-representing-1-to-n",
12220-
"PassRate": "59%",
12220+
"PassRate": "58%",
1222112221
"Difficulty": "Medium",
1222212222
"IsAccepted": true,
1222312223
"IsPaid": false,
@@ -12601,7 +12601,7 @@
1260112601
"ID": 1048,
1260212602
"Title": "Longest String Chain",
1260312603
"TitleSlug": "longest-string-chain",
12604-
"PassRate": "48%",
12604+
"PassRate": "49%",
1260512605
"Difficulty": "Medium",
1260612606
"IsAccepted": true,
1260712607
"IsPaid": false,
@@ -13237,7 +13237,7 @@
1323713237
"ID": 1101,
1323813238
"Title": "The Earliest Moment When Everyone Become Friends",
1323913239
"TitleSlug": "the-earliest-moment-when-everyone-become-friends",
13240-
"PassRate": "63%",
13240+
"PassRate": "64%",
1324113241
"Difficulty": "Medium",
1324213242
"IsAccepted": false,
1324313243
"IsPaid": true,
@@ -13465,7 +13465,7 @@
1346513465
"ID": 1120,
1346613466
"Title": "Maximum Average Subtree",
1346713467
"TitleSlug": "maximum-average-subtree",
13468-
"PassRate": "60%",
13468+
"PassRate": "61%",
1346913469
"Difficulty": "Medium",
1347013470
"IsAccepted": false,
1347113471
"IsPaid": true,
@@ -13777,7 +13777,7 @@
1377713777
"ID": 1146,
1377813778
"Title": "Snapshot Array",
1377913779
"TitleSlug": "snapshot-array",
13780-
"PassRate": "30%",
13780+
"PassRate": "31%",
1378113781
"Difficulty": "Medium",
1378213782
"IsAccepted": true,
1378313783
"IsPaid": false,
@@ -13789,7 +13789,7 @@
1378913789
"ID": 1147,
1379013790
"Title": "Longest Chunked Palindrome Decomposition",
1379113791
"TitleSlug": "longest-chunked-palindrome-decomposition",
13792-
"PassRate": "58%",
13792+
"PassRate": "57%",
1379313793
"Difficulty": "Hard",
1379413794
"IsAccepted": true,
1379513795
"IsPaid": false,
@@ -13825,7 +13825,7 @@
1382513825
"ID": 1150,
1382613826
"Title": "Check If a Number Is Majority Element in a Sorted Array",
1382713827
"TitleSlug": "check-if-a-number-is-majority-element-in-a-sorted-array",
13828-
"PassRate": "67%",
13828+
"PassRate": "66%",
1382913829
"Difficulty": "Easy",
1383013830
"IsAccepted": false,
1383113831
"IsPaid": true,
@@ -13945,7 +13945,7 @@
1394513945
"ID": 1160,
1394613946
"Title": "Find Words That Can Be Formed by Characters",
1394713947
"TitleSlug": "find-words-that-can-be-formed-by-characters",
13948-
"PassRate": "70%",
13948+
"PassRate": "69%",
1394913949
"Difficulty": "Easy",
1395013950
"IsAccepted": true,
1395113951
"IsPaid": false,
@@ -14041,7 +14041,7 @@
1404114041
"ID": 1168,
1404214042
"Title": "Optimize Water Distribution in a Village",
1404314043
"TitleSlug": "optimize-water-distribution-in-a-village",
14044-
"PassRate": "44%",
14044+
"PassRate": "45%",
1404514045
"Difficulty": "Hard",
1404614046
"IsAccepted": false,
1404714047
"IsPaid": true,

0 commit comments

Comments
 (0)