Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution { public: vector<vector<int> > generateMatrix(int n) { if (n == 0) return vector<vector<int> >(); static const int dy[] = {0,1,0,-1}; // right, down, left, up static const int dx[] = {1,0,-1,0}; vector<vector<int> > res; res.resize(n); for (int i = 0; i < n; ++i) res[i].resize(n,0); int cur = 1; for (int i = 0; i < n; ++i) res[0][i] = cur++; for (int i = 1; i < n; ++i) res[i][n-1] = cur++; for (int i = n-2; i >= 0; --i) res[n-1][i] = cur++; int y = n-1, x = 0, dir = 3; int t = n*n; while (cur <= t) { if (res[y+dy[dir%4]][x+dx[dir%4]] != 0) { dir++; continue; } y += dy[dir%4]; x += dx[dir%4]; res[y][x] = cur++; } return res; } };
@2013-10-03
class Solution { public: vector<vector<int> > generateMatrix(int n) { vector<vector<int> > res(n); for (int i = 0; i < n; i++) res[i].resize(n); gen(1, 0, n - 1, 0, n - 1, res); return res; } void gen(int start, int left, int right, int top, int bot, vector<vector<int> >& a) { if (left > right || top > bot) return; if (left == right) { a[left][top] = start ++; return; } else { for (int i = left; i <= right; i++) a[top][i] = start++; for (int i = top + 1; i < bot; i++) a[i][right] = start++; for (int i = right; i >= left; i--) a[bot][i] = start++; for (int i = bot - 1; i > top; i--) a[i][left] = start++; gen(start, left + 1, right - 1, top + 1, bot - 1, a); } } };
Given a matrix of m x n elements (m rows, n columns), 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]
.
class Solution { public: vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int> res; int n = matrix.size(); if (n == 0) return res; int m = matrix[0].size(); gen(res, 0, 0, n-1, m-1, matrix); return res; } void gen(vector<int>& res, int left, int top, int bottom, int right, vector<vector<int> >& a) { if (left > right || top > bottom) return; if (left == right) { for (int i = top; i <= bottom; i++) res.push_back(a[i][left]); return; } else if (top == bottom) { for (int i = left; i <= right; i++) res.push_back(a[top][i]); return; } else { for (int i = left; i <= right; i++ ) res.push_back(a[top][i]); for (int i = top + 1; i < bottom; i++) res.push_back(a[i][right]); for (int i = right; i >= left; i--) res.push_back(a[bottom][i]); for (int i = bottom - 1; i > top; i--) res.push_back(a[i][left]); gen(res, left + 1, top + 1, bottom - 1, right - 1, a); } } };
相关推荐
js js_leetcode题解之54-spiral-matrix.js
c是最好的编程语言 C语言_leetcode题解之54-spiral-matrix.c
js js_leetcode题解之59-spiral-matrix-II.js
c语言入门 C语言_leetcode题解之59-spiral-matrix-ii.c
15 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) [Python](./Python/3sum.py) | _O(n^2)_ | _O(1)_ | Medium || Two Pointers 16 | [3 Sum Closest]...
35. Spiral Matrix:螺旋遍历矩阵。 36. Integer to Roman:整数转换成罗马数字。 37. Roman to Integer:罗马数字转换成整数。 38. Clone Graph:深度复制一个图。 【栈】 39. Min Stack:设计一个栈,支持 push、...
- Spiral Matrix: 给定一个m×n矩阵,以螺旋方式遍历矩阵中的所有元素一次,并且只遍历一次。 - Merge Intervals: 给定一组区间,请合并所有重叠的区间。 - Insert Interval: 在一组已经排序的区间中,插入一个新的...
在本压缩包中,主题聚焦于C++编程基础与LeetCode题目的结合,特别是针对第54题“螺旋矩阵”(Spiral Matrix)的解法。LeetCode是一个在线平台,提供了一系列编程挑战,旨在帮助程序员提升算法技能和解决实际问题的...
第59题"螺旋矩阵II"(Spiral Matrix II)是LeetCode中的一个经典问题,它涉及到矩阵操作和迭代。在这个问题中,我们需要生成一个特定大小的螺旋矩阵,从中心向外螺旋式填充数字。 螺旋矩阵是一种特殊的二维数组,其...
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 ...
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 ...
leetcode 答案螺旋矩阵 返回二维数组中整数元素的顺时针螺旋列表 [答案击败 100% Java LeetCode 运行时提交] [答案击败 100% Java LeetCode 内存使用提交] 大(O)= O(N)
\n\n以下是一个简单的C语言代码示例,展示了如何生成螺旋矩阵:\n```c\n#include <stdio.h>\n\nvoid spiralOrder(int m, int n, int* matrix, int* nums) {\n int i, j, left = 0, right = m - 1, top = 0, bottom =...
function spiralOrder($matrix) { if (empty($matrix)) return []; $result = []; $left = 0; $right = count($matrix[0]) - 1; $top = 0; $bottom = count($matrix) - 1; while ($left $right && $top $...
标题中的“python-leetcode面试题解之第54题螺旋矩阵-题解.zip”表明这是一个关于Python编程语言的LeetCode面试题解答,具体是针对第54题——螺旋矩阵(Spiral Matrix)的解题代码和分析。LeetCode是一个在线平台,...
1 两和容易2 加两个数中5 最长回文子串中6 ZigZag 转换介质7 反转整数简单8 字符串到整数 (atoi) 中9 回文数容易11 盛水介质最多的容器12 整数到罗马中号13 罗马到整数简单14 最长公共前缀 简单15 3Sum 中18 4Sum 中...
多线程 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 ...
杂项部分包括了一些不那么容易归类的问题,如螺旋矩阵(Spiral Matrix)、整数转罗马数字(Integer to Roman)、克隆图(Clone Graph)等。 **栈(Stack)** 栈是一种先进后出(FILO)的数据结构。 - 最小栈(Min ...
leetcode LeetCode Solutions 算法 - Algorithms 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、剪枝技巧 图论:最短路、最小生成树、网络流建模 动态规划:背包问题、最长子序列、计数问题 基础...