http://oj.leetcode.com/problems/rotate-image/

将矩阵顺时针旋转90度。要求为原地算法。

注意对输入的测试,矩阵为空,长度为1,为2时……

#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
if(matrix.empty())
return;
vector<vector<int> >matrixB;
vector<int> onePiece; int len = matrix[].size();
if(len<=)
{
return;
}
for(int j = ;j<len;j++)
{
onePiece.clear();
for(int i = ;i<len;i++)
{
onePiece.push_back(matrix[len--i][j]);
}
matrixB.push_back(onePiece);
}
matrix = matrixB;
}
}; int main()
{
Solution myS;
vector<vector<int> > matrix;
vector<int> onePiece;
onePiece.clear();
onePiece.push_back();
matrix.push_back(onePiece);
/*onePiece.push_back(2);
onePiece.push_back(5);
matrix.push_back(onePiece);
onePiece.clear();
onePiece.push_back(3);
onePiece.push_back(4);
onePiece.push_back(6);
matrix.push_back(onePiece); onePiece.clear();
onePiece.push_back(8);
onePiece.push_back(9);
onePiece.push_back(10);*/
//matrix.push_back(onePiece); myS.rotate(matrix);
return ;
}

原地算法如下:

LeetCode OJ--Rotate Image的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  7. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  8. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  9. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. 使用linux安装gitolite管理git

    系统:centos7 服务器:阿里云 一.前期准备 1.安装git yum install git 2.安装perl yum install perl 3.安装openssh yum install ...

  2. Laravel中chunk组块结果集处理

    如果你需要处理成千上万个 Eloquent 结果,可以使用 chunk 命令.chunk 方法会获取一个“组块”的 Eloquent 模型,并将其填充到给定闭包进行处理.使用 chunk 方法能够在处 ...

  3. Html5_标签

    HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...

  4. hdu-2553 N皇后问题(搜索题)

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置方法. Inpu ...

  5. Aizu - 1386 Starting a Scenic Railroad Service (思维乱搞)

    给你n个区间,求: 1:最多有多少区间与同一个区间相交. 2:相交部分的最大区间数目. Sample Input 1 4 1 3 1 3 3 6 3 6 Sample Output 1 2 2 Sam ...

  6. MyEclipse访问MSSQL2008数据库

    首先到微软网站http://www.microsoft.com/zh-cn/download/details.aspx?id=21599下载sqljdbc_3.0.1301.101_chs.exe, ...

  7. 使用powershell/vbs自动化模拟鼠标点击操作

    今天想做windows上的自动化,所以才有了模拟鼠标点击的需求,先考虑用powershell实现: 首先先安装一个名为“WASP”免费可用的Powershell扩展程序,下载地址:http://was ...

  8. Leetcode 452.用最少数量的箭引爆气球

    用最少数量的箭引爆气球 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐 ...

  9. AtCoder Grand Contest 020

    A - Move and Win Time limit : 1sec / Memory limit : 512MB Score : 300 points Problem Statement A gam ...

  10. Java学习ing

    ConcurrentHashMap从JDK1.5开始随java.util.concurrent包一起引入JDK中,主要为了解决HashMap线程不安全和Hashtable效率不高的问题. Concur ...