当前位置:网站首页>剑指 Offer 29. 顺时针打印矩阵
剑指 Offer 29. 顺时针打印矩阵
2022-07-06 02:11:00 【anieoo】
原题链接;剑指 Offer 29. 顺时针打印矩阵
solution:
和螺旋矩阵一样的解法
class Solution {
public:
int dx[4] = {0, 1, 0, -1},dy[4] = {1, 0, -1, 0};
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res; //定义返回值
if(matrix.size() == 0) return res;
int m = matrix.size(),n = matrix[0].size();
vector<vector<bool>> sta(m, vector<bool> (n, false));
for(int i = 0,x = 0,y = 0,d = 0;i < m * n;i++) {
res.push_back(matrix[x][y]);
sta[x][y] = true;
int a = x + dx[d],b = y + dy[d];
if(a < 0 || a == m || b < 0 || b == n || sta[a][b]){
d = (d + 1) % 4;
a = x + dx[d],b = y + dy[d];
}
x = a,y = b;
}
return res;
}
};边栏推荐
- Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
- SSM 程序集
- Virtual machine network, networking settings, interconnection with host computer, network configuration
- Sword finger offer 12 Path in matrix
- 怎么检查GBase 8c数据库中的锁信息?
- Publish your own toolkit notes using NPM
- vs code保存时 出现两次格式化
- Lecture 4 of Data Engineering Series: sample engineering of data centric AI
- [solution] add multiple directories in different parts of the same word document
- Ali test open-ended questions
猜你喜欢

Use image components to slide through photo albums and mobile phone photo album pages

Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail

Redis如何实现多可用区?

在线怎么生成富文本

论文笔记: 图神经网络 GAT

Jisuanke - t2063_ Missile interception

Leetcode sum of two numbers

【社区人物志】专访马龙伟:轮子不好用,那就自己造!

2022年PMP项目管理考试敏捷知识点(8)

A basic lintcode MySQL database problem
随机推荐
【clickhouse】ClickHouse Practice in EOI
Redis daemon cannot stop the solution
Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
D22:indeterminate equation (indefinite equation, translation + problem solution)
阿裏測開面試題
2 power view
Numpy array index slice
Concept of storage engine
It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment
How to use C to copy files on UNIX- How can I copy a file on Unix using C?
02. Go language development environment configuration
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization
【coppeliasim】6自由度路径规划
Using SA token to solve websocket handshake authentication
[solution] add multiple directories in different parts of the same word document
Multi function event recorder of the 5th National Games of the Blue Bridge Cup
Kubernetes stateless application expansion and contraction capacity
leetcode-2. Palindrome judgment
Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle