48. Rotate Image
来源:互联网 发布:java ee api 编辑:程序博客网 时间:2024/06/05 08:58
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?
解题思路:先将元素按对角线来调换位置,然后按照列,将对称列元素全部交换
#include<iostream>#include<vector>using namespace std;class Solution {public: void rotate(vector<vector<int> >& matrix) { int i , j , n; n=matrix.size(); if(n == 0 || n == 1) return ; for(i = 0 ; i < n ; i++) { for(j=i+1;j<n;j++) swap(matrix[i][j],matrix[j][i]); } for(i=0;i<n/2;i++) { for(j=0;j<n;j++) swap(matrix[j][i],matrix[j][n-1-i]); } }};int main(){ int N,i,j,x; vector<vector<int> >matrix; vector<int>temp; cin>>N; for(i=0;i<N;i++) matrix.push_back(temp); for(i=0;i<N;i++) for(j=0;j<N;j++) { cin>>x; matrix[i].push_back(x); } Solution solve; solve.rotate(matrix); for(i=0;i<N;i++) { for(j=0;j<N;j++) cout<<matrix[i][j]<<' '; cout<<endl; } return 0;}
0 0
- [LeetCode]48.Rotate Image
- LeetCode 48. Rotate Image
- LeetCode 48.Rotate Image
- LeetCode --- 48. Rotate Image
- [Leetcode] 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- [leetcode] 48. Rotate Image
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [LeetCode]48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- leetcode 48. Rotate Image
- Leetcode 48. Rotate Image
- LeetCode 48. Rotate Image
- C++数组实现最简单的二叉树
- TensorFlow介绍_中英文对照
- 字母+数字验证码(二)
- 迷之四川省赛A题 Nearest Neighbor Search 数学题 国庆咸鱼
- 玩无限消乐的设计示例(高级篇)灭绝特效算法
- 48. Rotate Image
- (POJ 2528)Mayor's posters 线段树 + 离散化
- 通过终端shell来操作MySQL数据库(MAMP下)
- 【暂无】 二叉树 二叉树的计数(count-tree.cpp)
- Java学习总结第十四天Java类的继承
- Verify Preorder Sequence in Binary Search Tree
- 【二叉树】 堆应用:合并果子
- SHA1算法原理
- ARM嵌入式系统