【1.打印蛇形矩阵】
Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return[1,2,3,6,9,8,7,4,5]
.
【代码】
/**------------------------------------
* 日期:2015-02-05
* 作者:SJF0115
* 题目: 54.Spiral Matrix
* 网址:https://oj.leetcode.com/problems/spiral-matrix/
* 结果:AC
* 来源:LeetCode
* 博客:
---------------------------------------**/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> result;
if(matrix.empty()){
return result;
}//if
int row = matrix.size();
int col = matrix[0].size();
int count = row * col;
int index = 1;
int beginX = 0,endX = row - 1;
int beginY = 0,endY = col - 1;
while(index <= count){
// right
for(int i = beginY;i <= endY;++i){
result.push_back(matrix[beginX][i]);
++index;
}//for
++beginX;
if(beginX > endX){
break;
}//if
// down
for(int i = beginX;i <= endX;++i){
result.push_back(matrix[i][endY]);
++index;
}//for
--endY;
if(endY < beginY){
break;
}//if
// left
for(int i = endY;i >= beginY;--i){
result.push_back(matrix[endX][i]);
++index;
}//for
--endX;
if(endX < beginX){
break;
}//if
// up
for(int i = endX;i >= beginX;--i){
result.push_back(matrix[i][beginY]);
++index;
}
++beginY;
if(beginX > endY){
break;
}//if
}//while
return result;
}
};
int main(){
Solution s;
vector<vector<int> > matrix = {{1,2,3},{4,5,6},{7,8,9}};
vector<int> result = s.spiralOrder(matrix);
// 输出
for(int i = 0;i < result.size();++i){
cout<<result[i]<<" ";
}//for
cout<<endl;
return 0;
}
【2.生成蛇形矩阵】
Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.
For example,
Givenn=3
,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
【代码二】
/**------------------------------------
* 日期:2015-02-05
* 作者:SJF0115
* 题目: 59.Spiral Matrix II
* 网址:https://oj.leetcode.com/problems/spiral-matrix-ii/
* 结果:AC
* 来源:LeetCode
* 博客:
---------------------------------------**/
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > matrix(n,vector<int>(n,0));
if(n <= 0){
return matrix;
}//if
int count = n * n;
int index = 1;
int beginX = 0,endX = n - 1;
int beginY = 0,endY = n - 1;
while(index <= count){
// right
for(int i = beginY;i <= endY;++i){
matrix[beginX][i] = index;
++index;
}//for
++beginX;
// down
for(int i = beginX;i <= endX;++i){
matrix[i][endY] = index;
++index;
}//for
--endY;
// left
for(int i = endY;i >= beginY;--i){
matrix[endX][i] = index;
++index;
}//for
--endX;
// up
for(int i = endX;i >= beginX;--i){
matrix[i][beginY] = index;
++index;
}
++beginY;
}//while
return matrix;
}
};
【代码三】
/**------------------------------------
* 日期:2015-02-04
* 作者:SJF0115
* 题目: 59.Spiral Matrix II
* 网址:https://oj.leetcode.com/problems/spiral-matrix-ii/
* 结果:AC
* 来源:LeetCode
* 博客:
---------------------------------------**/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > matrix(n,vector<int>(n,0));
if(n <= 0){
return matrix;
}//if
int count = n * n;
int index = 1;
int x = 0,y = -1;
while(index <= count){
// right
++y;
while(y < n && matrix[x][y] == 0){
matrix[x][y++] = index;
++index;
}//while
--y;
// down
++x;
while(x < n && matrix[x][y] == 0){
matrix[x++][y] = index;
++index;
}//while
--x;
// left
--y;
while(y >= 0 && matrix[x][y] == 0){
matrix[x][y--] = index;
++index;
}//while
++y;
// up
--x;
while(x >= 0 && matrix[x][y] == 0){
matrix[x--][y] = index;
++index;
}//while
++x;
}//while
return matrix;
}
};
int main(){
Solution s;
int n = 5;
vector<vector<int> > matrix = s.generateMatrix(n);
// 输出
for(int i = 0;i < n;++i){
for(int j = 0;j < n;++j){
cout<<matrix[i][j]<<" ";
}//for
cout<<endl;
}//for
return 0;
}
分享到:
相关推荐
嵌入式经典面试题嵌入式经典面试题嵌入式经典面试题嵌入式经典面试题嵌入式经典面试题
10万字208道Java经典面试题总结(附答案).pdf 10万字208道Java经典面试题总结(附答案).pdf 10万字208道Java经典面试题总结(附答案).pdf 10万字208道Java经典面试题总结(附答案).pdf 10万字208道Java经典面试题总结(附...
Python经典面试题 Python经典面试题 python面试题 python 面试题 Python经典面试题 Python经典面试题 python面试题 python 面试题 Python经典面试题 Python经典面试题 python面试题 python 面试题 Python经典面试题 ...
C ++经典面试题C ++经典面试题C ++经典面试题C ++经典面试题C ++经典面试题C ++经典面试题C ++经典面试题
PHP经典面试题 PHP经典面试题 PHP经典面试题 PHP经典面试题 PHP经典面试题
SQL经典面试题及答案 SQL经典面试题及答案
面试管经常文的Matlab面试题算法矩阵详情加解答2023.docx面试管经常文的Matlab面试题算法矩阵详情加解答2023.docx面试管经常文的Matlab面试题算法矩阵详情加解答2023.docx面试管经常文的Matlab面试题算法矩阵详情加...
vue经典面试题及答案.rar vue经典面试题及答案.rar vue经典面试题及答案.rar vue经典面试题及答案.rar vue经典面试题及答案.rar vue经典面试题及答案.rar vue经典面试题及答案.rar vue经典面试题及答案.rar vue经典...
面试时整理的资料,大家可以看看,面试的java题目,面试时整理的资料,大家可以看看,面试的java题目
2022年SQL数据库经典面试题笔试题 (2).pdf2022年SQL数据库经典面试题笔试题 (2).pdf2022年SQL数据库经典面试题笔试题 (2).pdf2022年SQL数据库经典面试题笔试题 (2).pdf2022年SQL数据库经典面试题笔试题 (2).pdf2022...
java经典面试题(典藏版)java经典面试题(典藏版)java经典面试题(典藏版)java经典面试题(典藏版)java经典面试题(典藏版)
50+Vue经典面试题详解,你值得收藏.50+Vue经典面试题详解,你值得收藏.50+Vue经典面试题详解,你值得收藏.50+Vue经典面试题详解,你值得收藏.50+Vue经典面试题详解,你值得收藏.50+Vue经典面试题详解,你值得收藏.50...
50+Vue经典面试题详解,你值得收藏.docx50+Vue经典面试题详解,你值得收藏.docx50+Vue经典面试题详解,你值得收藏.docx50+Vue经典面试题详解,你值得收藏.docx50+Vue经典面试题详解,你值得收藏.docx50+Vue经典面试...
C++经典面试题 C++经典面试题 C++经典面试题 C++经典面试题 C++经典面试题
各公司经典面试题选取
IT行业经典面试题,121套面试题 IT行业经典面试题,121套面试题 IT行业经典面试题,121套面试题 IT行业经典面试题,121套面试题
cobol面试题 cobol面试题 cobol面试题
C++经典面试题2 C++经典面试题2 C++经典面试题2 C++经典面试题2 C++经典面试题2
Spring经典面试题Spring经典面试题Spring经典面试题Spring经典面试题
sqlserver 经典面试题。详细说明面试重点,值得参考