Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 2326 #979

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added leetcode/2301-2400/2326.Spiral-Matrix-IV/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/2301-2400/2326.Spiral-Matrix-IV/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 21 additions & 14 deletions leetcode/2301-2400/2326.Spiral-Matrix-IV/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [2326.Spiral Matrix IV][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given two integers `m` and `n`, which represent the dimensions of a matrix.

You are also given the `head` of a linked list of integers.

Generate an `m x n` matrix that contains the integers in the linked list presented in **spiral order **(clockwise)**, starting from the **top-left** of the matrix. If there are remaining empty spaces, fill them with ``-1`.

Return the generated matrix.

**Example 1:**
**Example 1:**

![1](./1.jpg)

```
Input: a = "11", b = "1"
Output: "100"
Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
Explanation: The diagram above shows how the values are printed in the matrix.
Note that the remaining spaces in the matrix are filled with -1.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.jpg)

### 思路1
> ...
Spiral Matrix IV
```go
```

Input: m = 1, n = 4, head = [0,1,2]
Output: [[0,1,2,-1]]
Explanation: The diagram above shows how the values are printed from left to right in the matrix.
The last space in the matrix is set to -1.
```

## 结语

Expand Down
40 changes: 38 additions & 2 deletions leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package Solution

func Solution(x bool) bool {
return x
type ListNode struct {
Val int
Next *ListNode
}

func Solution(m int, n int, head *ListNode) [][]int {
ans := make([][]int, m)
for i := range m {
ans[i] = make([]int, n)
for j := range n {
ans[i][j] = -1
}
}

x, y := 0, 0
width := 0
for head != nil {
for ; y < n-width && head != nil; y, head = y+1, head.Next {
ans[x][y] = head.Val
}
x, y = x+1, y-1

for ; x < m-width && head != nil; x, head = x+1, head.Next {
ans[x][y] = head.Val
}
x, y = x-1, y-1

for ; y >= width && head != nil; y, head = y-1, head.Next {
ans[x][y] = head.Val
}
x, y = x-1, y+1
for ; x > width && head != nil; x, head = x-1, head.Next {
ans[x][y] = head.Val
}
width++
x, y = width, width
}
return ans
}
26 changes: 16 additions & 10 deletions leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,36 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
m, n int
head *ListNode
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, 5, &ListNode{3, &ListNode{0, &ListNode{2, &ListNode{6, &ListNode{8, &ListNode{1, &ListNode{7, &ListNode{9, &ListNode{4, &ListNode{2, &ListNode{5, &ListNode{5, &ListNode{0, nil}}}}}}}}}}}}}, [][]int{
{3, 0, 2, 6, 8},
{5, 0, -1, -1, 1},
{5, 2, 4, 9, 7},
}},
{"TestCase2", 1, 4, &ListNode{0, &ListNode{1, &ListNode{2, nil}}}, [][]int{
{0, 1, 2, -1},
}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.m, c.n, c.head)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.m, c.n, c.head)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading