[LeetCode] Rotate Image n-by-n矩阵顺时针旋转
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
一题严格的数组逻辑操作问题,考虑认真就好,思路:
| 1 | 2 | 3 | 1 |
| 3 | 4 | 4 | 2 |
| 2 | 4 | 4 | 3 |
| 1 | 3 | 2 | 1 |
如上表,一共有两圈,对于n 来说,圈数为(n+1)/2,从外圈到内圈,转换了一圈后到下一圈。
#include <iostream>
#include <vector>
#include <iterator>
using namespace std; class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n =matrix.size();
if(n<) return ;
for(int i=;i<(n+)/;i++){
for(int len=;len<=n-*(i+);len++){
int temp = matrix[i][i+len];
matrix[i][i+len] = matrix[n--i-len][i];
matrix[n--i-len][i] = matrix[n--i][n--i-len];
matrix[n--i][n--i-len] = matrix[i+len][n-i-];
matrix[i+len][n-i-] = temp;
}
}
}
}; int main()
{
vector<vector<int> > matrix;
vector<int> tempm;
int cnt =;
for(int i=;i<;i++){
for(int j=;j<;j++)
tempm.push_back(cnt++);
matrix.push_back(tempm);
tempm.clear();
}
Solution sol;
sol.rotate(matrix);
for(int i=;i<matrix.size();i++){
copy(matrix[i].begin(),matrix[i].end(),ostream_iterator<int>(cout," "));
cout<<endl;
} return ;
}
[LeetCode] Rotate Image n-by-n矩阵顺时针旋转的更多相关文章
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- Rotate Image,N*N矩阵顺时针旋转90度
public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && mat ...
- LeetCode Rotate Image (模拟)
题意: 将一个n*n的矩阵顺时针旋转90度. 思路: 都是差不多的思路,交换3次也行,反转再交换也是行的. class Solution { public: void rotate(vector< ...
- 将n*n矩阵顺时针旋转90度
/** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- LeetCode——Rotate Image
1. Question You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
随机推荐
- shell时间变量拼接问题
shell时间变量拼接问题 例1 ABC=ABC_`date –date='yesterday' "+%Y%m%d"`
- Object-C知识点 (六) 开发中的技巧
本文主要介绍开发中的一些实用技巧 #pragma mark - 代码控制Home按键 [[UIApplication sharedApplication] performSelector:@selec ...
- jquery源码学习第一天
第一天认识了jquery的大体结构,总的大范围是 (function() { // 这里是封装的代码,包括了各种方法.工具 window.JQuery = JQuery window.$ = $; } ...
- Golang ioutil读写文件测试
运用 ioutil.ReadFile .ioutil.WriteFile package main import ( "io/ioutil" "log" &qu ...
- Pycharm安装类库
比如安装requests 打开settings,选择Project 下面的Project Interpreter,点击pip,在弹出窗口里输入requests然后点击install 即可!
- svn提交报错,提示:locked,需要cleanup
版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/p/9285137.html 在使用SVN提交代码或更新代码时经常会 ...
- vim小操作
初时,先有ed,ed为ex之父,ex为vi之父,而vi为vim之父 c 修改 d 删除 y 复制到寄存器 g~ 反转大小写 gu 反转为小写 gU 反转为大写 > 增加缩进 < 减小缩进 ...
- destoon 后台入口文件weigouadmin.php解析
destoon有几个文件不能修改,一修改后台就无法登陆,weigouadmin.php就是其中之一,据官网客服说这个文件是可以修改的,不知为什么即使不修改打开一下保存后后台就不能登陆了.因刚接触dt, ...
- python向上取整 向下取整
向上取整 ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数. ceil()是不能直接访问的,需要导入 math 模块. import math math.ceil( x ) ...
- uncaught exception 'NSInternalInconsistencyException, reason:[UITableViewController loadView] loaded the "Controller" nib but didn't get a UITableView
http://blog.csdn.net/ryantang03/article/details/7941058#reply 上面那篇文章是我查找的ios实现下拉刷新功能,在我下载完代码运行的过程中发现 ...