Leetcode_Rotate Image
来源:互联网 发布:网络赚钱项目有哪些 编辑:程序博客网 时间:2024/03/29 10:21
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?
说实在的,这不能算是原创,因为虽然代码是我自己写的,但这个思路,我得承认我是剽窃了discuss里的
题目本身的难点在于限制了空间的使用,就不能再另开数组了。自己想的时候,想到一圈一圈用异或解决问题。
但是翻看了discuss以后,发现了一种神奇的办法:先把数组上下翻转,然后再沿主对角线翻转...就OK了,不信那张纸转转就知道了
因为90度旋转,如果我们以水平为x,垂直为y,坐标原点在左上角,那么目标就是(i,j)=>(n-j,i),上下反转(i,j)=>(i,n-j),对角线翻转(i,n-j)=>(n-j,i)完成任务
惊了,思路上还是得多学习啊...
class Solution {public: void rotate(vector<vector<int> > &matrix) { //上下翻转,然后沿主对角线翻转,就OK了... //谁想到的这方法..... int size = matrix.size(); if(size<=1){ return; } reverse(matrix.begin(),matrix.end()); for(int i=0;i<size;++i){ for(int j=i+1;j<size;++j){ swap(matrix[i][j],matrix[j][i]); } } }};
0 0
- LeetCode_Rotate Image
- Leetcode_rotate-image
- Leetcode_Rotate Image
- LeetCode_Rotate Image
- leetcode_Rotate Image
- LeetCode_rotate-image
- Leetcode_rotate-list
- LeetCode_Rotate List
- leetcode_Rotate Array
- Leetcode_Rotate Array
- Image
- Image
- Image
- Image
- image
- image
- image
- image
- cfg80211的kernel架构(基于Linux 3.08)之sta
- Android 4.2蓝牙介绍
- oracle 聚簇表
- 隐藏在背后的交互设计
- herf js 提示
- Leetcode_Rotate Image
- rosserial_embeddedlinux编译
- Easyui 用datagrid 查询数据
- 链表《5》使用链表实现学生成绩管理系统
- javascript jquery基本用法 增删改查
- 测试类型和说明
- 怎么利用wireshark人为制造丢包情况
- 如何使用google的日志库(glog)
- 如何恢复工作中清除的照片