国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

leetcode 59. Spiral Matrix II

2019-11-06 06:35:45
字體:
來源:轉載
供稿:網友
package com.bupt;//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[][] result = new int[n][n];		 int number = 1;		 int minLength = 0;		 int maxLength = n-1;		 while(number<=n*n){			 int index = minLength;			 while(index <= maxLength){				 result[minLength][index] = number;				 number++;				 index++;			 }			 minLength++;			 index = minLength;			 while(index<=maxLength){				 result[index][maxLength] = number;				 number++;				 index++;			 }			 maxLength--;			 minLength--;			 index = maxLength;			 while(index>=minLength){				 result[maxLength+1][index] = number;				 number++;				 index--;			 }			 minLength++;			 index = maxLength;			 while(index>=minLength){				 result[index][minLength-1] = number;				 number++;				 index--;			 }		 }		 return result;	 }        public static void main(String[] args){    	Solution s = new Solution();    	int[][] output = s.generateMatrix(1);    	for(int i = 0;i<output.length;i++){    		for(int j = 0;j<output.length;j++){    			System.out.PRint(output[i][j]);    		}    		System.out.println();    	}    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天全县| 北安市| 宁河县| 仪征市| 宁德市| 武冈市| 建昌县| 马关县| 兴仁县| 连南| 商都县| 咸丰县| 邵武市| 桐乡市| 股票| 石门县| 新巴尔虎右旗| 久治县| 额敏县| 衡阳市| 蒲江县| 宝坻区| 蒙城县| 华坪县| 兰州市| 赞皇县| 新民市| 峨山| 甘德县| 桂东县| 武川县| 延吉市| 安福县| 海城市| 阳朔县| 河东区| 建瓯市| 原平市| 原平市| 鞍山市| 原平市|