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 ] ]
public class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; int cnt = 1; int top = 0; int bottom = n - 1; int left = 0; int right = n - 1; while (left < right && top < bottom) { for (int i = left; i < right; i++) { res[top][i] = cnt++; } for (int i = top; i < bottom; i++) { res[i][right] = cnt++; } for (int i = right; i > left; i--) { res[bottom][i] = cnt++; } for (int i = bottom; i > top; i--) { res[i][left] = cnt++; } left++; right--; top++; bottom--; } if (n % 2 != 0) { res[n/2][n/2] = cnt; } return res; } }
相关推荐
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 微博: @...
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/...
第 338 章leetcode_cpp leetcode 的 C++ 代码 包含的问题 1 两和容易2 加两个数中5 最长回文子串中6 ZigZag 转换介质7 反转整数简单8 ...II ...Spiral Matrix II 培养基61 轮播列表中第62话第63话64 最小路径和中66
对于每一道算法题会总结代码、时间复杂度以及一些好的blog排序(sort)...Spiral Matrix IILeetCode 53 Maximum SubarrayLeetCode 152 Maximum Product SubarrayLintCode 138 Subarray SumLintCode 139 Subarray Sum ...
螺旋矩阵螺旋矩阵项目介绍从矩阵的中间开始,按顺时针方向向右...要求必需的阿帕奇Maven Java JDK 7建造mvn clean install package && cd target && java -jar spin-matrix-1.0.jar && cd ..常问问题执照 Copyright
螺旋矩阵给你一个正整数n,生成一个包含1到n2所有元素,且元素按顺时针顺序螺旋排列的nxn正方形矩阵。
螺旋矩阵的输出 C++实现源程序 Spiral matrix output C++ achieve source
Done for programming class. Generates a spiral matrix.
任务 : 编写一个程序,该程序以顺时针螺旋顺序打印/返回2D矩阵的元素。抽象的: 就性能而言,编写短代码并不总是意味着它是高质量的代码。 同样,编写更长的代码并不意味着它是不好的代码! Numpy数组由于其同质...
leetcode 答案螺旋矩阵 返回二维数组中整数元素的顺时针螺旋列表 [答案击败 100% Java LeetCode 运行时提交] [答案击败 100% Java LeetCode 内存使用提交] 大(O)= O(N)
java lru leetcode LeetCode Solutions 算法 - Algorithms 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、剪枝技巧 图论:最短路、最小生成树、网络流建模 动态规划:背包问题、最长子序列、计数...
螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10....
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 ...
兼容性------------- Matlab 7.2 及更高版本笔记----------- 此函数利用函数 matrix_perimeter.m 例子------------ >> A=魔法(5) 一 = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 >> ...
多线程 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
这是我准备面试时建议的个人问题清单。...https://leetcode.com/problems/spiral-matrix/ https://leetcode.com/problems/search-a-2d-matrix/ https://leetcode.com/problems/set-matrix-zeroes/ 弦乐 ...
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
class SpiralIteratorMatrix holds the data of a matrix It uses createIterator() to create an pointer of its iterator. By using this iterator, it can loops through all its element in the clockwise ...
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 ...
java lru leetcode LeetCode 记录数据结构与算法/LeetCode练习过程,将...Spiral Matrix Mergesort [Algorithm Swap](Mergesort/Algorithm swap.md) Numbers Queue Stack Toposort Trie Tree Two Pointers Union Find