LeeCode-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 ]
]
/**
* Return an array of arrays.
* Note: The returned array must be malloced, assume caller calls free().
*/
int** generateMatrix(int n) {
int **Maxtrix; Maxtrix=(int **)malloc(n*sizeof(int*));
for(int k=;k<n;k++)
Maxtrix[k]=(int *)malloc(sizeof(int)*n); int number = ;
int top = ;
int bottom = n-;
int left = ;
int right = n-; int i,j;
while(number<=n*n)
{
for(i=left;i<=right;i++)
Maxtrix[top][i]=number++;
top++; for(i=top;i<=bottom;i++)
Maxtrix[i][right]=number++;
right--; for(i=right;i>=left;i--)
Maxtrix[bottom][i]=number++;
bottom--; for(i=bottom;i>=top;i--)
Maxtrix[i][left]=number++;
left++; }
return Maxtrix;
}
LeeCode-Spiral Matrix II的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 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] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- Largest Rectangle in Histogram 解答
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
- xcode6和ios 8 百度无法定位解决
. @interface里: CLLocationManager *locationManager; . 初始化: locationManager = [[CLLocationManager allo ...
- Android学习总结——去除标题栏
1.继承app.Activity的Activity去除标题栏 @Override protected void onCreate(Bundle savedInstanceState) { super. ...
- KafkaOffsetMonitor监控
介绍 KafkaOffsetMonitor是有由Kafka开源社区提供的一款Web管理界面,这个应用程序用来实时监控Kafka服务的Consumer以及它们所在的Partition中的Offset,你 ...
- 解开Android应用程序组件Activity的"singleTask"之谜
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6714543 在Android应用程序中,可以配 ...
- Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...
- linux下git使用记录1 git 提交
linux下git使用记录1 浏览:985 发布日期:2013/08/08 分类:技术分享 在使用github的时候,不可避免的接触到了git,用他来更新项目,做版本控制.这里特别把常用的命令记录 ...
- jquery模仿css3延迟效果
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- AngularJs学习(1)
以下是学习过程中的笔记,有些是网上摘录 <!DOCTYPE HTML> <html lang="zh-cn"> <head> <meta ...
- eclipse中多个工程编译到同一个目录下
1.点击link source 2.选择Java(ps:Java文件目录)或者resource(ps:配置文件目录) 3.最后结果,然后使用project中的clean进行编译,就可以把两个工程编 ...