首页 > 其他 > 详细

LeetCode -- Spiral Matrix II

时间:2015-10-14 01:37:55      阅读:207      评论:0      收藏:0      [点我收藏+]
题目描述:


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 ]
]


给定一个数字,由外到内生成矩阵。


思路:


本题主要是完成一个旋转的过程,遍历时track圈(cycle)的宽度,分为从左到右,上到下,右到左,左到上四个不同方向。每次cycle递减。
其次要注意n为奇偶数的cycle值的不同。




实现代码:


public class Solution {
    public int[,] GenerateMatrix(int n) {
        var matrix = new int[n,n];
	
    	var row = n % 2 == 0 ? n / 2 : (n+1) / 2;
    	var count = 1;
    	for(var cycle = 0;cycle < row; cycle++){
    		
    		// left to right
    		for(var col = cycle;col < n - cycle - 1; col++){
    			matrix[cycle,col] = count ++;
    		}
    		
    		// right to bottom
    		for(var r = cycle ; r < n - cycle; r++){
    			matrix[r, n - cycle - 1] = count ++;
    		}
    		
    		// bottom to left
    		for(var col = n - cycle - 2;col >= cycle; col --){
    			matrix[n-cycle-1,col] = count ++;
    		}
    		
    		// left to top
    		for(var r = n - cycle - 2;r > cycle; r --){
    			matrix[r,cycle] = count ++;
    		}
    	}
    	
    	return matrix;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode -- Spiral Matrix II

原文:http://blog.csdn.net/lan_liang/article/details/49108417

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!