59. Spiral Matrix II(中等,同54题)
Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
思路完全同 54. Spiral Matrix(中等).
自个代码:
vector<vector<int>> generateMatrix(int n) {
	vector<vector<int>> A(n, vector<int>(n)); //n行n列,动态的
	// Normal Case
	int rowStart = 0;
	int rowEnd = n - 1;
	int colStart = 0;
	int colEnd = n - 1;
	int num = 1; //change
	while (rowStart <= rowEnd && colStart <= colEnd) {
		for (int i = colStart; i <= colEnd; i++) {
			A[rowStart][i] = num++; //change
		}
		rowStart++;
		for (int i = rowStart; i <= rowEnd; i++) {
			A[i][colEnd] = num++; //change
		}
		colEnd--;
		for (int i = colEnd; i >= colStart; i--) {
			if (rowStart <= rowEnd)
				A[rowEnd][i] = num++; //change
		}
		rowEnd--;
		for (int i = rowEnd; i >= rowStart; i--) {
			if (colStart <= colEnd)
				A[i][colStart] = num++; //change
		}
		colStart++;
	}
	return A;
}
59. Spiral Matrix II(中等,同54题)的更多相关文章
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
		54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ... 
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
		54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ... 
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ... 
- 【leetcode】59.Spiral Matrix II
		Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ... 
- LeetCode: 59. Spiral Matrix II(Medium)
		1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ... 
- Leetcode#59 Spiral Matrix II
		原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ... 
- 59. Spiral Matrix II
		题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ... 
- 【一天一道LeetCode】#59. Spiral Matrix II
		一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ... 
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
		Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ... 
随机推荐
- GIT入门笔记(13)- GUI GIT
- 在Debian或Ubuntu中安装和使用'搜狗输入法for linux'
			下载搜狗输入法 for linux点击 搜狗输入法 for linux 以下载安装包到本地 安装搜狗输入法 for linuxA.准备工作: (1) 连接网络.挂载系统安装盘 此安装过程需要网络连接, ... 
- python/*args和**kwargs
			*args和**kwargs #coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值 ... 
- uva 10917 Walk Through The Forest
			题意: 一个人从公司回家,他可以从A走到B如果从存在从B出发到家的一条路径的长度小于任何一条从A出发到家的路径的长度. 问这样的路径有多少条. 思路: 题意并不好理解,存在从B出发到家的一条路径的长度 ... 
- C#:多进程开发,控制进程数量
			正在c#程序优化时,如果多线程效果不佳的情况下,也会使用多进程的方案,如下: System.Threading.Tasks.Task task=System.Threading.Tasks.Task. ... 
- 多线程编程、java图形用户界面编程、Java I / O系统
			线程概述 进程:是一种 “自包容”的运行程序 线程是进程当中的一个概念,最小处理单位 THread类.Runnable接口.Object类 创建新执行线程有两种方法:1:一种方法是将类声明为Threa ... 
- Flume报 Space for commit to queue couldn't be acquired. Sinks are likely not keeping up with sources, or the buffer size is too tight
			报这个错误 需要一个是flume堆内存不够.还有一个就是把channel的容器调大 在channel加配置 type - 组件类型名称必须是memory capacity 100 存储在 Channe ... 
- 使用Vertx重构系统小结
			背景 前几个月,使用Vertx重构了公司的一个子系统,该系统负责公司核心数据subscriber的采集.处理.存储和搜索.这里介绍下重构该系统时的一些关键点. 架构 重构前系统部署图: 重构前系统主要 ... 
- javax.el.ELException: Error reading [name] on type [com.news.entity.Topic_$$_javassist_1]异常
			异常如下: 异常分析:从message中可以看出,错误是读取异常,属性是name,路径是com.news.entity.Topic,此错误是使用Hibernate时,由于Hibernate还没有去数据 ... 
- [LeetCode] Add Bold Tag in String 字符串中增添加粗标签
			Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and ... 
