-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ou 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: 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: 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. Constraints: 1 <= m, n <= 105 1 <= m * n <= 105 The number of nodes in the list is in the range [1, m * n]. 0 <= Node.val <= 1000
- Loading branch information
1 parent
ee396f5
commit fa38ffc
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package _2326 | ||
|
||
type ListNode struct { | ||
Val int | ||
Next *ListNode | ||
} | ||
|
||
func spiralMatrix(m int, n int, head *ListNode) [][]int { | ||
resultMatrix := make([][]int, m) | ||
for i := 0; i < m; i++ { | ||
resultMatrix[i] = make([]int, n) | ||
for j := 0; j < n; j++ { | ||
resultMatrix[i][j] = -1 | ||
} | ||
} | ||
|
||
top, bottom, left, right := 0, m-1, 0, n-1 | ||
|
||
for head != nil && top <= bottom && left <= right { | ||
for i := left; i <= right && head != nil; i++ { | ||
resultMatrix[top][i] = head.Val | ||
head = head.Next | ||
} | ||
top++ | ||
|
||
for i := top; i <= bottom && head != nil; i++ { | ||
resultMatrix[i][right] = head.Val | ||
head = head.Next | ||
} | ||
right-- | ||
|
||
if top <= bottom { | ||
for i := right; i >= left && head != nil; i-- { | ||
resultMatrix[bottom][i] = head.Val | ||
head = head.Next | ||
} | ||
} | ||
bottom-- | ||
|
||
if left <= right { | ||
for i := bottom; i >= top && head != nil; i-- { | ||
resultMatrix[i][left] = head.Val | ||
head = head.Next | ||
} | ||
} | ||
left++ | ||
} | ||
|
||
return resultMatrix | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package _2326 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_spiralMatrix(t *testing.T) { | ||
type args struct { | ||
m int | ||
n int | ||
head *ListNode | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want [][]int | ||
}{ | ||
{ | ||
name: "Example 1", | ||
args: args{ | ||
m: 3, | ||
n: 3, | ||
head: &ListNode{ | ||
Val: 1, | ||
Next: &ListNode{ | ||
Val: 2, | ||
Next: &ListNode{ | ||
Val: 3, | ||
Next: &ListNode{ | ||
Val: 4, | ||
Next: &ListNode{ | ||
Val: 5, | ||
Next: &ListNode{ | ||
Val: 6, | ||
Next: &ListNode{ | ||
Val: 7, | ||
Next: &ListNode{ | ||
Val: 8, | ||
Next: &ListNode{ | ||
Val: 9, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
want: [][]int{ | ||
{1, 2, 3}, | ||
{8, 9, 4}, | ||
{7, 6, 5}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := spiralMatrix(tt.args.m, tt.args.n, tt.args.head); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("spiralMatrix() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkSpiralMatrix(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
spiralMatrix(3, 3, &ListNode{ | ||
Val: 1, | ||
Next: &ListNode{ | ||
Val: 2, | ||
Next: &ListNode{ | ||
Val: 3, | ||
Next: &ListNode{ | ||
Val: 4, | ||
Next: &ListNode{ | ||
Val: 5, | ||
Next: &ListNode{ | ||
Val: 6, | ||
Next: &ListNode{ | ||
Val: 7, | ||
Next: &ListNode{ | ||
Val: 8, | ||
Next: &ListNode{ | ||
Val: 9, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
} |