lintcode: 旋转图像
旋转图像
给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像。
解题
顺时针旋转90度 就是 上下翻转,再主对角对折
public class Solution {
/**
* @param matrix: A list of lists of integers
* @return: Void
*/
public void rotate(int[][] A) {
// write your code here
if (A == null || A.length == 0 || A[0].length == 0)
return ;
int m = A.length;
int n = A[0].length;
// 上下翻转 后 主对角翻转
// 上下翻转
for(int i = 0;i<= (m-1)/2;i++){
for(int j = 0;j< n;j++){
int tmp = A[i][j];
A[i][j] = A[m - i -1][j];
A[m - i -1][j] = tmp;
}
}
// 主对角翻转
for(int i = 0;i< m;i++ ){
for(int j=i+1;j< n;j++){
int tmp = A[i][j];
A[i][j] = A[j][i];
A[j][i] = tmp;
}
}
}
}
lintcode: 旋转图像的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Lintcode 372. O(1)时间复杂度删除链表节点
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...
- Lintcode 469. 等价二叉树
----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...
随机推荐
- Apple Watch应用开发经验谈:我遇到的那些坑
本文作者张忠良是滴答清单Apple Watch版应用的开发工程师,他用了一周的时间使用纯Objective-C语言完成了Apple Watch版滴答清单应用的开发工作.在这里,他从开发角度阐述了个人对 ...
- Mac下显示\隐藏所有文件
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏Mac隐藏文件的命令:defaults write ...
- java程序用做windows服务
具体步骤在这里 http://www.doc88.com/p-360144091164.html 遇到错误: JVM did not exit on request, terminated 通过下面的 ...
- ThinkPHP技巧
在php文件可以用 echo D_S()->getLastSql();来打印出 当前的sql语句
- asp.net 间传值的方法
1.页面间使用post请求时,可以使用Request.Form["xxx"] 2.QueryString 3.Context上下文传值,使用在Server.Transfer中,这个 ...
- IOS常用加密GTMBase64
GTMDefines.h // // GTMDefines.h // // Copyright 2008 Google Inc. // // Licensed under the Apache Lic ...
- python 单元测试-unittest
参考资料:https://docs.python.org/3.4/library/unittest.html#module-unittest 一张图解决问题: 涉及5块内容:case.suite.lo ...
- Codeforces Round #109 (Div. 2) E. Double Profiles hash
题目链接: http://codeforces.com/problemset/problem/155/E E. Double Profiles time limit per test 3 second ...
- 控制UIlabel 垂直方向对齐方式的 方法
最正统的方法,利用objective-c的category特性,修改UILabel的绘制代码.示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- 用npm安装express后express命令找不到
Windows 平台加了 npm install -g express 也不行AppData\Roaming\npm 下面没有 express.bat 解决办法: sudo npm install - ...