LeetCode OJ:Rotate Image(旋转图片)
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?
题目要就地算法,那么可以通过折叠矩阵上下以及斜对角线折叠来实现,代码如下:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int sz = matrix.size();
if(!sz) return;
for(int i = ; i < sz; ++i){
for(int begin = , end = sz - ; begin < end; ++begin, --end){
swap(matrix[begin][i], matrix[end][i]);
}
}
for(int i = ; i < sz; ++i){
for(int j = i; j < sz; ++j){
swap(matrix[i][j], matrix[j][i]);
}
}
}
};
同样的通过乘以一个矩阵的方法当然也可以实现,不过那不是in-place算法。
LeetCode OJ:Rotate Image(旋转图片)的更多相关文章
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- [Leetcode] rotate image 旋转图片
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- [leetcode]61. Rotate List旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- rotate.js实现图片旋转 (chrome,IE,firefox都可以实现)
找了好多资料,要么是IE可以用,但是谷歌不行,,还有就是两个都可以用的,图片大小显示不全.终于找到一个好一点的js,先贴一下代码. 1.rotate.js jQuery.fn.rotate = fun ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- LeetCode 48. Rotate Image(旋转图像)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode] 48. Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- npm更新和nodejs更新
npm更新和nodejs更新 更新你已经安装的NPM库,这个很简单,只需要运行. npm update -g 更新Nodejs自身.一直依赖我都是下载最新版的源码,然后make install,及其繁 ...
- loadrunner winsocket sent buffer 乱码
data.ws里手写的xml参数,调试脚本时一直显示乱码,解决方法如下: tools-recording options--sockets--winsock下: EBCID--translation ...
- VS2015配置安卓Android和iOS开发环境
http://www.cjjjs.cn/paper/gzsh/627201502818357.aspx [摘要] 本文按照步骤一步步的介绍要下载安装的东西,都提供了下载地址.最后将所有需要的程序都打包 ...
- Unity 自定义编辑窗体之ScriptableWizard
当我们在编辑界面要批量设置游戏资源的时候,就需要从UnityEditor里面继承,实现自己的窗口类. 所幸UNITY提供了最简单的一个自定义窗体类,我们直接往上扔public类型的属性就好,提供了确认 ...
- 一个用于实现并行执行的 Java actor 库
即使 Java 6 和 Java 7 中引入并发性更新,Java 语言仍然无法让并行编程变得特别容易.Java 线程.synchronized 代码块.wait/notify 和java.util.c ...
- js踩过的一些坑
参考我的博客:http://www.isedwardtang.com/2017/08/29/js-bug/
- C++中的config设计
配置文件读写类,它要有以下这些方法: 1. 支持读入一个指定配置文件的能力 2. 支持随时加入一个配置项的能力 3. 足够强大,能够写入各种数据结构的配置信息 C++ 里,我们要存储这样的数据就使用 ...
- Java:正则表达式
Java:正则表达式 package com.fsti.icop.util.regexp; import java.util.regex.Matcher; import java.util.regex ...
- 基于HTML5和SVG的手机菜单动画
在线演示 本地下载
- [CTSC2008]祭祀river
Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都 会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组 ...