当前位置:网站首页>2020-09-03:裸写算法:回形矩阵遍历。
2020-09-03:裸写算法:回形矩阵遍历。
2020-11-06 21:50:00 【福大大架构师每日一题】
福哥答案2020-09-03:
方法一:模拟,位图方式。
跟 方法二 一样,区别是辅助矩阵visited用位图节约空间。
方法二:模拟。
可以模拟螺旋矩阵的路径。初始位置是矩阵的左上角,初始方向是向右,当路径超出界限或者进入之前访问过的位置时,则顺时针旋转,进入下一个方向。
判断路径是否进入之前访问过的位置需要使用一个与输入矩阵大小相同的辅助矩阵visited,其中的每个元素表示该位置是否被访问过。当一个元素被访问时,将 visited 中的对应位置的元素设为已访问。
如何判断路径是否结束?由于矩阵中的每个元素都被访问一次,因此路径的长度即为矩阵中的元素数量,当路径的长度达到矩阵中的元素数量时即为完整路径,将该路径返回。
复杂度分析
时间复杂度:O(mn),其中 m 和 n 分别是输入矩阵的行数和列数。矩阵中的每个元素都要被访问一次。
空间复杂度:O(mn)。需要创建一个大小为 m×n 的矩阵 visited 记录每个位置是否被访问过。
方法三:按层模拟
可以将矩阵看成若干层,首先输出最外层的元素,其次输出次外层的元素,直到输出最内层的元素。
定义矩阵的第 k 层是到最近边界距离为 k 的所有顶点。例如,下图矩阵最外层元素都是第 1 层,次外层元素都是第 2 层,剩下的元素都是第 3 层。
复杂度分析
时间复杂度:O(mn),其中 m 和 n 分别是输入矩阵的行数和列数。矩阵中的每个元素都要被访问一次。
空间复杂度:O(1)。除了输出数组以外,空间复杂度是常数。
代码用golang编写,如下:
package test37_spiralorder
import (
"fmt"
"testing"
)
//https://leetcode-cn.com/problems/spiral-matrix/solution/luo-xuan-ju-zhen-by-leetcode-solution/
//go test -v -test.run TestSpiralOrder
func TestSpiralOrder(t *testing.T) {
const N = 3
matrix := make([][]int, N)
for i := 0; i < N; i++ {
matrix[i] = make([]int, N)
for j := 0; j < N; j++ {
matrix[i][j] = i*N + j + 1
}
}
fmt.Println(matrix)
ret := spiralOrder1(matrix)
fmt.Println(ret, "方法一:模拟,位图")
ret = spiralOrder2(matrix)
fmt.Println(ret, "方法二:模拟")
ret = spiralOrder3(matrix)
fmt.Println(ret, "方法三:按层模拟")
}
//方法一:模拟,位图
func spiralOrder1(matrix [][]int) []int {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return []int{}
}
rows, columns := len(matrix), len(matrix[0])
visited := make([]byte, (rows*columns+7)/8)
var (
total = rows * columns
order = make([]int, total)
row, column = 0, 0
directions = [][]int{[]int{0, 1}, []int{1, 0}, []int{0, -1}, []int{-1, 0}}
directionIndex = 0
)
for i := 0; i < total; i++ {
order[i] = matrix[row][column]
SetBitMapValue(visited, row*columns+column, true)
nextRow, nextColumn := row+directions[directionIndex][0], column+directions[directionIndex][1]
if nextRow < 0 ||
nextRow >= rows ||
nextColumn < 0 ||
nextColumn >= columns ||
GetBitMapValue(visited, nextRow*columns+nextColumn) {
directionIndex = (directionIndex + 1) % 4
}
row += directions[directionIndex][0]
column += directions[directionIndex][1]
}
return order
}
//方法二:模拟
func spiralOrder2(matrix [][]int) []int {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return []int{}
}
rows, columns := len(matrix), len(matrix[0])
visited := make([][]bool, rows)
for i := 0; i < rows; i++ {
visited[i] = make([]bool, columns)
}
var (
total = rows * columns
order = make([]int, total)
row, column = 0, 0
directions = [][]int{[]int{0, 1}, []int{1, 0}, []int{0, -1}, []int{-1, 0}}
directionIndex = 0
)
for i := 0; i < total; i++ {
order[i] = matrix[row][column]
visited[row][column] = true
nextRow, nextColumn := row+directions[directionIndex][0], column+directions[directionIndex][1]
if nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn] {
directionIndex = (directionIndex + 1) % 4
}
row += directions[directionIndex][0]
column += directions[directionIndex][1]
}
return order
}
//方法三:按层模拟
func spiralOrder3(matrix [][]int) []int {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return []int{}
}
var (
rows, columns = len(matrix), len(matrix[0])
order = make([]int, rows*columns)
index = 0
left, right, top, bottom = 0, columns - 1, 0, rows - 1
)
for left <= right && top <= bottom {
for column := left; column <= right; column++ {
order[index] = matrix[top][column]
index++
}
for row := top + 1; row <= bottom; row++ {
order[index] = matrix[row][right]
index++
}
if left < right && top < bottom {
for column := right - 1; column > left; column-- {
order[index] = matrix[bottom][column]
index++
}
for row := bottom; row > top; row-- {
order[index] = matrix[row][left]
index++
}
}
left++
right--
top++
bottom--
}
return order
}
//获取位图第index元素的值
func GetBitMapValue(data []byte, index int) bool {
return data[index/8]&(1<<(index%8)) != 0
}
//设置位图第index元素的值
func SetBitMapValue(data []byte, index int, v bool) {
if v {
data[index/8] |= 1 << (index % 8)
} else {
data[index/8] &= ^(1 << (index % 8))
}
}
敲 go test -v -test.run TestSpiralOrder 命令,结果如下:
版权声明
本文为[福大大架构师每日一题]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4553401/blog/4544055
边栏推荐
- 開源一套極簡的前後端分離專案腳手架
- What are PLC Analog input and digital input
- (2) ASP.NET Core3.1 Ocelot routing
- ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
- Some operations kept in mind by the front end foundation GitHub warehouse management
- Application of restful API based on MVC
- nacos、ribbon和feign的簡明教程
- electron 實現檔案下載管理器
- To Lianyun analysis: why is IPFs / filecoin mining so difficult?
- Details of dapr implementing distributed stateful service
猜你喜欢
window系统 本机查找端口号占用方法
华为Mate 40 系列搭载HMS有什么亮点?
list转换map(根据key来拆分list,相同key的value为一个list)
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
Python basic data type -- tuple analysis
ES6 learning notes (5): easy to understand ES6's built-in extension objects
MongoDB与SQL常用语法对应表
Ronglian completed US $125 million f round financing
The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures
统计项目代码行数
随机推荐
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures
An article takes you to understand CSS3 picture border
Isn't data product just a report? absolutely wrong! There are university questions in this category
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
常用SQL语句总结
Use modelarts quickly, zero base white can also play AI!
IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
Metersphere developer's Manual
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Details of dapr implementing distributed stateful service
Behind the first lane level navigation in the industry
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
Description of phpshe SMS plug-in
Elasticsearch Part 6: aggregate statistical query
每个大火的“线上狼人杀”平台,都离不开这个新功能
What are PLC Analog input and digital input