You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lavos/cap3/work2.go

42 lines
860 B

package cap3
// Work2 返回给定数组的偏移布局矩阵
func Work2(nums []int) [][]int {
n := len(nums)
result := make([][]int, n)
for i := 0; i < n; i++ {
result[i] = make([]int, n)
}
colStart := 0
for _, num := range nums {
for row, col := 0, colStart; row < n; row++ {
result[row][col%n] = num
col++
}
colStart++
}
return result
}
// Work2type2 返回给定数组的偏移布局矩阵,另一种写法
func Work2type2(nums []int) [][]int {
n := len(nums)
result := make([][]int, n)
for i := 0; i < n; i++ {
result[i] = make([]int, n)
}
offset := 0
for row := 0; row < n; row++ {
for col := 0; col < n; col++ {
result[row][(offset+col)%n] = nums[col]
}
offset++
}
return result
}