LeetCode OJ--Rotate Image
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的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 初涉网络流[EK&dinic]
主要还是板子 Edmonds-Karp 从S开始bfs,直到找到一条到达T的路径后将该路径增广,并重复这一过程. 在处理过程中,为了应对“找到的一条路径把其他路径堵塞”的情况,采用了建反向弧的方式来实 ...
- Django ORM (三) 查询,删除,更新操作
ORM 查询操作 修改 views.py 文件 from django.shortcuts import render, HttpResponse from app01 import models f ...
- list 方法总结整理
#!/usr/bin/env python #Python 3.7.0 列表常用方法 __author__ = "lrtao2010" #创建列表 # a = [] # b = [ ...
- Solr 中的 docValues=true
前言: 在Lucene4.x之后,出现一个重大的特性,就是索引支持DocValues,这对于广大的solr和elasticsearch用户,无疑来说是一个福音,这玩意的出现通过牺牲一定的磁盘空间带来 ...
- IIS发布网站Microsoft JET Database Engine 错误 '80004005'的解决办法,基于Access数据库
在网站发布后,访问网站会有80004005的错误提示. 项目环境 项目基于Access数据库,server2012,文件系统为NTFS格式. 错误信息 Microsoft JETDatabase En ...
- 重新造轮子之静态链接1(Static linking)
最近学习计算机病毒学的过程中,又讲到了静态链接的问题,联想到了之前保健哥在信息安全的课堂上向我们展示了一个没有main()函数的C程序到底应该如何编写.个人觉得这个小实验对于加深静态链接的过程的理解也 ...
- HDU 5399 数学 Too Simple
题意:有m个1~n的映射,而且对于任意的 i 满足 f1(f2(...fm(i))) = i 其中有些映射是知道的,有些是不知道的,问一共有多少种置换的组合. 分析: 首先这些置换一定是1~n的一个置 ...
- webdriver高级应用- 禁止IE的保护模式
#encoding=utf-8 from selenium import webdriver from selenium.webdriver.common.desired_capabilities i ...
- Selenium WebDriver-模拟鼠标双击某个元素
#encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...
- Leetcode 416.分割等和子集
分割等和子集 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 注意: 每个数组中的元素不会超过 100 数组的大小不会超过 200 示例 1: 输入: [ ...