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

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

Spiral Matrix II

2019-11-14 09:17:41
字體:
來源:轉載
供稿:網友

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

vector<vector<int> > fun(int n){	vector<vector<int> > result(n, vector<int>(n));	int left = 0;	int right = n-1;	int top = 0;	int bottom = n-1;	int count = 0;	int total = n*n;	while (count < total)	{		for (int i = left; i <= right && count < total; i++)		{			count++;			result[top][i] = count;		}		top++;		for (int i = top; i <= bottom && count < total; i++)		{			count++;			result[i][right] = count;		}		right--;		for (int i = right; i >= left && count < total; i--)		{			count++;			result[bottom][i] = count;		}		bottom--;		for (int i = bottom; i >= top && count < total; i--)		{			count++;			result[i][left] = count;		}		left++;	}	return result;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苍梧县| 华池县| 姚安县| 平遥县| 湘阴县| 来凤县| 高邑县| 武汉市| 七台河市| 藁城市| 苗栗县| 宁河县| 二连浩特市| 班玛县| 越西县| 郧西县| 原平市| 建阳市| 屯留县| 永胜县| 广昌县| 疏勒县| 西华县| 古浪县| 延寿县| 丰都县| 长乐市| 通江县| 昭通市| 旺苍县| 莱西市| 怀来县| 讷河市| 兴海县| 卢氏县| 乌拉特前旗| 澜沧| 潜山县| 炎陵县| 资阳市| 大冶市|