diff --git a/leetcode/2301-2400/2326.Spiral-Matrix-IV/1.jpg b/leetcode/2301-2400/2326.Spiral-Matrix-IV/1.jpg new file mode 100644 index 000000000..34bcd8c03 Binary files /dev/null and b/leetcode/2301-2400/2326.Spiral-Matrix-IV/1.jpg differ diff --git a/leetcode/2301-2400/2326.Spiral-Matrix-IV/2.jpg b/leetcode/2301-2400/2326.Spiral-Matrix-IV/2.jpg new file mode 100644 index 000000000..8d5161324 Binary files /dev/null and b/leetcode/2301-2400/2326.Spiral-Matrix-IV/2.jpg differ diff --git a/leetcode/2301-2400/2326.Spiral-Matrix-IV/README.md b/leetcode/2301-2400/2326.Spiral-Matrix-IV/README.md index dc72e7cdd..57e93999e 100755 --- a/leetcode/2301-2400/2326.Spiral-Matrix-IV/README.md +++ b/leetcode/2301-2400/2326.Spiral-Matrix-IV/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution.go b/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution.go index d115ccf5e..8be4f3350 100644 --- a/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution.go +++ b/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution.go @@ -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 } diff --git a/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution_test.go b/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution_test.go index 14ff50eb4..af9b89bf7 100644 --- a/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution_test.go +++ b/leetcode/2301-2400/2326.Spiral-Matrix-IV/Solution_test.go @@ -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() { }