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. JDBC-防止SQL注入问题

      String sql = "select * from user where name = '" + name + "' and password = '" ...

  2. 多本Python极速入门最佳书籍,不可错过的Python学习资料!

    Python作为现在很热门的一门编程语言,介于Python的友好,许多的初学者都将其作为首选,为了帮助大家更好的学习Python,我筛选了2年内优秀的python书籍,个别经典的书籍扩展到5年内.   ...

  3. nrf51822微信开发2:[转]airkiss/airsync介绍

    "微信蓝牙"专题共分为8部分 1.airkiss/airsync介绍 2.eclipes的j2ee软件使用教程 3.微信公众号使用Dome(airkiss/airsync) 4.新 ...

  4. 用\r做出进度条

    在做ftp作业的时候,需要做一个上传和下载的进度条,做的时候发现用\r很容易就能做出来 def show_progress(self, has, total): rate = float(has) / ...

  5. 剑指Offer(书):顺时针打印数组

    题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1 ...

  6. 【HIHOCODER 1589】回文子串的数量(Manacher)

    描述 给定一个字符串S,请统计S的所有|S| * (|S| + 1) / 2个子串中(首尾位置不同就算作不同的子串),有多少个是回文字符串? 输入 一个只包含小写字母的字符串S. 对于30%的数据,S ...

  7. POJ:1751-Highways(Kruskal和Prim)

    Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6078 Accepted: 1650 Special Judg ...

  8. 水题:51Nod 1163-最高的奖励

    最高的奖励 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 Description 有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励.在结束时间之前完成该任 ...

  9. Django Model two

    Django_model: eg: class XXXX(models.Model): nid = models.AutoField(primary_Key=True) name = models.C ...

  10. Selenium WebDriver- 指定页面加载时间

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...