【题目】
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-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;
}
【代码二】
/**------------------------------------
* 日期: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;
}
};
分享到:
相关推荐
462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...
leetcode 答案螺旋矩阵 返回二维数组中整数元素的顺时针螺旋列表 [答案击败 100% Java LeetCode 运行时提交] [答案击败 100% Java LeetCode 内存使用提交] 大(O)= O(N)
Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of ...
这是我准备面试时建议的个人问题清单。...https://leetcode.com/problems/spiral-matrix/ https://leetcode.com/problems/search-a-2d-matrix/ https://leetcode.com/problems/set-matrix-zeroes/ 弦乐 ...
leetcode 530 力码 全部的: 易(173/237+x) 中(144/437+x) 硬(4/x) 问题 1.Two Sum(dict) 7.(跳过)(数学) 9.(跳过)(串串技巧...59.Spiral Matrix 2(蛮力) 62.独特的路径 63/62.Unique Paths 2 64.最小路径和
leetcode LeetCode Solutions 算法 - Algorithms 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、剪枝技巧 图论:最短路、最小生成树、网络流建模 动态规划:背包问题、最长子序列、计数问题 基础...
leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...
第 338 章leetcode_cpp leetcode 的 C++ 代码 包含的问题 1 两和容易2 加两个数中5 最长回文子串中6 ZigZag 转换介质7 ...II ...59 Spiral Matrix II 培养基61 轮播列表中第62话第63话64 最小路径和中66
多线程 leetcode 前言 ...Spiral Matrix Path Sum II Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water
Spiral Matrix medium O 66 Plus One easy O O 118 Pascal's Triangle easy O O 119 Pascal's Triangle II easy O 要满足只用一个array大小空间O(k) k为input大小来完成,须具备backtracking概念 151 Reverse Words ...
对于每一道算法题会总结代码、时间复杂度以及一些好的blog排序(sort)...59 Spiral Matrix IILeetCode 53 Maximum SubarrayLeetCode 152 Maximum Product SubarrayLintCode 138 Subarray SumLintCode 139 Subarray ...
java lru leetcode LeetCode 记录数据结构与算法/LeetCode练习过程,将...Spiral Matrix Mergesort [Algorithm Swap](Mergesort/Algorithm swap.md) Numbers Queue Stack Toposort Trie Tree Two Pointers Union Find
Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on a Line Community QQ 群: 237669375 Github: https://www.github.com/soulmachine/algorithm-essentials 微博: @...
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix:return [] m,n = len(matrix),len(matrix[0]) x = y = di = 0 dx = [0,1,0,-1] dy = [1,0,-1,0] res = [] visite