Skip to content

Commit 9b92ec5

Browse files
committed
[E:82/539, M:76/986, H:10/393] add No: 86: Partition List
1 parent e73e2fc commit 9b92ec5

File tree

13 files changed

+477
-0
lines changed

13 files changed

+477
-0
lines changed

Diff for: .leetcode.db

0 Bytes
Binary file not shown.

Diff for: questions/serial/medium/86/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## [分隔链表](https://leetcode-cn.com/problems/partition-list/)
2+
3+
给你一个链表和一个特定值 __ `x` ,请你对链表进行分隔,使得所有小于 `x` 的节点都出现在大于或等于 `x` 的节点之前。
4+
5+
你应当保留两个分区中每个节点的初始相对位置。
6+
7+
 
8+
9+
**示例:**
10+
11+
`
12+
**输入:**head = 1->4->3->2->5->2, _x_ = 3
13+
**输出:**1->2->2->4->3->5
14+
`

Diff for: questions/serial/medium/86/golang/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/medium/86/golang/solution"
11+
"github.com/gladmo/leetcode/utils/list"
12+
)
13+
14+
func main() {
15+
16+
tests := []struct {
17+
name string
18+
input1 *list.Node
19+
input2 int
20+
want string
21+
}{
22+
{
23+
name: "test-[1,4,3,2,5,2]-3",
24+
input1: list.CreateNode("[1,4,3,2,5,2]"),
25+
input2: 3,
26+
want: "[1,2,2,4,3,5]",
27+
},
28+
}
29+
30+
testLog := leet.NewTestLog(len(tests))
31+
defer testLog.Render()
32+
33+
timeoutDuration := time.Second * 2
34+
35+
for idx, test := range tests {
36+
// 超时检测
37+
got := test.want
38+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
39+
got = solution.Export(test.input1, test.input2).String()
40+
cancel()
41+
})
42+
43+
if timeout {
44+
testLog.Fail(idx+1, test.name, "timeout")
45+
continue
46+
}
47+
48+
if !reflect.DeepEqual(test.want, got) {
49+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
50+
continue
51+
}
52+
53+
testLog.Pass(idx+1, test.name)
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
8+
"github.com/gladmo/leetcode/utils/list"
9+
)
10+
11+
type ListNode = list.Node
12+
13+
func Export(head *ListNode, x int) *ListNode {
14+
defer func() {
15+
if r := recover(); r != nil {
16+
fmt.Println("Params: ", head, x)
17+
fmt.Println("Panic:", r)
18+
fmt.Println()
19+
debug.PrintStack()
20+
os.Exit(0)
21+
}
22+
}()
23+
24+
return partition(head, x)
25+
}
26+
27+
/****************************************************/
28+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
29+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
30+
/****************************************************/
31+
32+
/**
33+
* Definition for singly-linked list.
34+
* type ListNode struct {
35+
* Val int
36+
* Next *ListNode
37+
* }
38+
*/
39+
func partition(head *ListNode, x int) *ListNode {
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
8+
"github.com/gladmo/leetcode/utils/list"
9+
)
10+
11+
type ListNode = list.Node
12+
13+
func Export(head *ListNode, x int) *ListNode {
14+
defer func() {
15+
if r := recover(); r != nil {
16+
fmt.Println("Params: ", head, x)
17+
fmt.Println("Panic:", r)
18+
fmt.Println()
19+
debug.PrintStack()
20+
os.Exit(0)
21+
}
22+
}()
23+
24+
return partition(head, x)
25+
}
26+
27+
/****************************************************/
28+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
29+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
30+
/****************************************************/
31+
32+
/**
33+
* Definition for singly-linked list.
34+
* type ListNode struct {
35+
* Val int
36+
* Next *ListNode
37+
* }
38+
*/
39+
func partition(head *ListNode, x int) *ListNode {
40+
41+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## [分隔链表](https://leetcode-cn.com/problems/partition-list/)
2+
3+
给你一个链表和一个特定值 __ `x` ,请你对链表进行分隔,使得所有小于 `x` 的节点都出现在大于或等于 `x` 的节点之前。
4+
5+
你应当保留两个分区中每个节点的初始相对位置。
6+
7+
 
8+
9+
**示例:**
10+
11+
`
12+
**输入:**head = 1->4->3->2->5->2, _x_ = 3
13+
**输出:**1->2->2->4->3->5
14+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/questions/tags/双指针/medium/partition-list/golang/solution"
10+
"github.com/gladmo/leetcode/leet"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[1,4,3,2,5,2]
17+
3
18+
19+
*/
20+
21+
tests := []struct {
22+
name string
23+
input [][]int
24+
want bool
25+
}{
26+
{
27+
name: "test-[[1],[2],[3],[]]",
28+
input: [][]int{
29+
{1},
30+
{2},
31+
{3},
32+
{},
33+
},
34+
want: true,
35+
},
36+
}
37+
38+
testLog := leet.NewTestLog(len(tests))
39+
defer testLog.Render()
40+
41+
timeoutDuration := time.Second * 2
42+
43+
for idx, test := range tests {
44+
// 超时检测
45+
got := test.want
46+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
47+
got = solution.Export(test.input)
48+
cancel()
49+
})
50+
51+
if timeout {
52+
testLog.Fail(idx+1, test.name, "timeout")
53+
continue
54+
}
55+
56+
if !reflect.DeepEqual(test.want, got) {
57+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
58+
continue
59+
}
60+
61+
testLog.Pass(idx+1, test.name)
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
10+
type ListNode struct {
11+
Val int
12+
Next *ListNode
13+
}
14+
15+
func Export(head *ListNode, x int) *ListNode {
16+
defer func() {
17+
if r := recover(); r != nil {
18+
fmt.Println("Params: ", head, x)
19+
fmt.Println("Panic:", r)
20+
fmt.Println()
21+
debug.PrintStack()
22+
os.Exit(0)
23+
}
24+
}()
25+
26+
return partition(head, x)
27+
}
28+
29+
/****************************************************/
30+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
31+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
32+
/****************************************************/
33+
34+
/**
35+
* Definition for singly-linked list.
36+
* type ListNode struct {
37+
* Val int
38+
* Next *ListNode
39+
* }
40+
*/
41+
func partition(head *ListNode, x int) *ListNode {
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
10+
type ListNode struct {
11+
Val int
12+
Next *ListNode
13+
}
14+
15+
func Export(head *ListNode, x int) *ListNode {
16+
defer func() {
17+
if r := recover(); r != nil {
18+
fmt.Println("Params: ", head, x)
19+
fmt.Println("Panic:", r)
20+
fmt.Println()
21+
debug.PrintStack()
22+
os.Exit(0)
23+
}
24+
}()
25+
26+
return partition(head, x)
27+
}
28+
29+
/****************************************************/
30+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
31+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
32+
/****************************************************/
33+
34+
/**
35+
* Definition for singly-linked list.
36+
* type ListNode struct {
37+
* Val int
38+
* Next *ListNode
39+
* }
40+
*/
41+
func partition(head *ListNode, x int) *ListNode {
42+
43+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## [分隔链表](https://leetcode-cn.com/problems/partition-list/)
2+
3+
给你一个链表和一个特定值 __ `x` ,请你对链表进行分隔,使得所有小于 `x` 的节点都出现在大于或等于 `x` 的节点之前。
4+
5+
你应当保留两个分区中每个节点的初始相对位置。
6+
7+
 
8+
9+
**示例:**
10+
11+
`
12+
**输入:**head = 1->4->3->2->5->2, _x_ = 3
13+
**输出:**1->2->2->4->3->5
14+
`

0 commit comments

Comments
 (0)