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 ...
随机推荐
- k8s的资源限制及资源请求
容器的资源需求及限制: 需求:requests ##定义容器运行时至少需要资源 限制:limits ##定义容器运行时最多能分配的资源 requests:pod.spec.con ...
- 配置基于Vim的Python开发环境
配置基于Vim的Python开发环境插件 Vundle YouCompleteMe NERDTree Vim-Jinja2-Syntax set nocompatible " be iMpr ...
- thinkphp5开发restful-api接口学习 教程视频 接口文档
目录 1. 获取验证码 2. 用户注册 3. 用户登录 4. 用户上传头像 5. 用户修改密码 6. 用户找回密码 7. 用户绑定手机号 8. 用户绑定邮箱 9. 用户绑定用户名(手机/邮箱) 10. ...
- leetcode-24-exercise
506. Relative Ranks 解题思路: 使用priority_queue.它在插入时会将数据按照由大到小的顺序插入,自然排序了.所以插入时考虑插入pair<nums[i],i> ...
- IAR生成bin,HEX文件
1.生成bin,hex文件 options->output converter->output format binary:.bin文件:intel extended:hex文件. 生成的 ...
- LeetCode(151) Reverse Words in a String
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- 【HIHOCODER 1182】欧拉路·三
描述 小Hi和小Ho破解了一道又一道难题,终于来到了最后一关.只要打开眼前的宝箱就可以通关这个游戏了. 宝箱被一种奇怪的机关锁住: 这个机关是一个圆环,一共有2^N个区域,每个区域都可以改变颜色,在黑 ...
- poj 3669 火星撞地球问题 bfs算法
题意:火星撞地球,你要跑到一个永远安全的地方,求最短时间 思路:bfs+预处理 这题的数据量比较大,所以需要进行预处理 对每个位置设上时间(被撞的最早时间) 未被撞的设为-1 for (int j = ...
- JVM执行子系统探究——类文件结构初窥
类文件(.class)是搞java的都非常熟悉的文件,一般我们在编写java之后文件之后,首先通过javac工具生成.class类字节码文件,而后在执行程序的时候由虚拟机加载执行.那么为什么要生成.c ...
- 《Scrum实战》第3次课【富有成效的每日站会】作业汇总
1组 崔儒: http://kecyru.blog.163.com/blog/static/2741661732017626101944123/ 2017-07-26 孟帅: http://www.c ...