Add Biweekly 40

This commit is contained in:
YDZ
2020-11-29 20:20:08 +08:00
parent 9ac3fdeb96
commit efbd8e4156
4 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeInBetween(list1 *ListNode, a int, b int, list2 *ListNode) *ListNode {
pre, cur, list2Cur := list1, list1.Next, list2
for cur.Next != nil {
if cur.Val == a {
pre.Next = list2
pre = cur
break
}
pre = cur
cur = cur.Next
}
cur = cur.Next
for list2Cur.Next != nil {
list2Cur = list2Cur.Next
}
if a == b {
list2Cur.Next = cur
return list1
}
for cur.Next != nil {
if cur.Val == b {
list2Cur.Next = cur.Next
break
}
pre = cur
cur = cur.Next
}
return list1
}

View File

@ -0,0 +1,62 @@
package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question2 struct {
para2
ans2
}
// para 是参数
// one 代表第一个参数
type para2 struct {
one []int
a int
b int
another []int
}
// ans 是答案
// one 代表第一个答案
type ans2 struct {
one []int
}
func Test_Problem2(t *testing.T) {
qs := []question2{
{
para2{[]int{0, 1, 2, 3, 4, 5}, 3, 4, []int{1000000, 1000001, 1000002}},
ans2{[]int{0, 1, 2, 1000000, 1000001, 1000002, 5}},
},
{
para2{[]int{0, 1, 2, 3, 4, 5, 6}, 2, 5, []int{1000000, 1000001, 1000002, 1000003, 1000004}},
ans2{[]int{0, 1, 1000000, 1000001, 1000002, 1000003, 1000004, 6}},
},
{
para2{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 3, 5, []int{1000000, 1000001, 1000002, 1000003, 1000004, 1000005, 1000006}},
ans2{[]int{0, 1, 2, 1000000, 1000001, 1000002, 1000003, 1000004, 1000005, 1000006, 6, 7, 8, 9}},
},
{
para2{[]int{0, 1, 2}, 1, 1, []int{1000000, 1000001, 1000002, 1000003}},
ans2{[]int{0, 1000000, 1000001, 1000002, 1000003, 2}},
},
}
fmt.Printf("------------------------Leetcode Problem 2------------------------\n")
for _, q := range qs {
_, p := q.ans2, q.para2
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(mergeInBetween(structures.Ints2List(p.one), p.a, p.b, structures.Ints2List(p.another))))
}
fmt.Printf("\n\n\n")
}