LeetCode(59)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 ]
]
分析
与54题Spiral Matrix相似题,为一个二维矩阵进行螺旋状赋值
AC代码
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int> > ret(n,vector<int>(n , 0));
if (n <= 0)
return ret;
int index = 1 , row = n-1 , col = n-1;
for (int x = 0, y = 0; x <= row && y <= col; x++, y++)
{
//为矩阵首行赋值
for (int j = y; j <= col; ++j , index++)
ret[x][j] = index;
//为矩阵最右列赋值
for (int i = x + 1; i <= row; ++i,index++)
ret[i][col] = index;
//为矩阵最底行赋值
for (int j = col - 1; j >= y && x != row; --j, index++)
ret[row][j] = index;
//为矩阵最左列赋值
for (int i = row - 1; i > x && y != col; --i, index++)
ret[i][y] = index;
//为内旋子矩阵赋值
row--;
col--;
}//for
return ret;
}
};
LeetCode(59)SPiral Matrix II的更多相关文章
- LeetCode(54)Spiral Matrix
题目 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(73)Set Matrix Zeroes
题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cli ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- Leetcode(59)-Count Primes
题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非 ...
随机推荐
- Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏
使用Latex可以排版出漂亮的论文,尤其适合对含有数学公式论文的排版. 下面编写第一Latex源文件,实现对两个数学公式的排版: 新建文件first.tex: \documentclass{artic ...
- 官方XmlPullParser和网络解析xml示例及详述
Parsing XML Data This lesson teaches you to Choose a Parser Analyze the Feed Instantiate the Parser ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
- 基于Windows7下snort+apache+php 7 + acid(或者base) + adodb + jpgraph的入侵检测系统的搭建(图文详解)(博主推荐)
为什么,要写这篇论文? 是因为,目前科研的我,正值研三,致力于网络安全.大数据.机器学习.人工智能.区域链研究领域! 论文方向的需要,同时不局限于真实物理环境机器实验室的攻防环境.也不局限于真实物理机 ...
- 设置webbrowser浏览器内核
var hklm = Microsoft.Win32.Registry.LocalMachine; var lmRun64 = hklm.OpenSubKey(@"SO ...
- 公众号如何获取已关注用户的unionid的问题
避免误导,先加一句:首先,得公众号绑定开放平台 这个问题困扰了我一早上,我尝试了很多次获取unionid都失败. 微信的开发文档上有说: 关于特殊场景下的静默授权 1.上面已经提到,对于以snsapi ...
- APK瘦身-是时候给App进行减负了
前言 APK瘦身即是对APK大小进行压缩策略,减小APK安装包大小,更小的安装包更有助于吸引用户安装.前一段时间我司某一App进行APK的瘦身,最终也达到了减小10M的目标,现做一个简单的总结记录. ...
- Android基础夯实--重温动画(五)之属性动画 ObjectAnimator详解
只有一种真正的英雄主义 一.摘要 ObjectAnimator是ValueAnimator的子类,它和ValueAnimator一样,同样具有计算属性值的功能,但对比ValueAnimator,它会更 ...
- 阿里云服务器安装ss使用
下载安装服务器版shadowsocks yum install epel-release yum update yum install python-setuptools m2crypto super ...
- GA详解
转:http://blog.csdn.net/u010451580/article/details/51178225 本文是去年课题组周报中的一个专题讲解,详细讲了GA,由于是周报,所以十分详细.很适 ...