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 ...
随机推荐
- Python 抓取网页乱码问题 以及EXCEL乱码
import codecs f1=codecs.open('items.json', 'r', encoding='utf-8').read().decode("unicode_escape ...
- Android实现SharePreferences和AutoCompletedTextView
Android实现SharePreferences和AutoCompletedTextView 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 ...
- 消息点击事件的响应链---hitTest:withEvent:方法
*当用户点击屏幕时,会产生一个触摸事件,系统会将触摸事件加入到 UIApplication管理事件队里中 *UIApplication 会从事件队列中取出最前面的事件进行分发以便处理,通常,先发送事件 ...
- HDU 5695 Gym Class 拓扑排序
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5695 题解: 求出字典序最大的拓扑序.然后把求好的数列翻转过来就是满足条件的数列,然后模拟求一下va ...
- Entity Framework公共的增删改方法
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.I ...
- Ubuntu 下编译安装linux
1. 准备工作切换为管理员权限,sudo –i 输入用户密码 进入root 权限apt-get install build-essential kernel-package libncurses5-d ...
- mac上安装port
今天和大家分享下 在mac os x 上安装port 本人在 OS X El Capitan 版本的操作系统上进行安装并测试. --------------------------------- 第 ...
- cf 496B Secret Combination
题目链接:B. Secret Combination You got a box with a combination lock. The lock has a display showing n d ...
- 迭代启发式搜索 IDA*
本章聚集了一些做了的迭代启发式搜索的题目 为什么只打了迭代启发式搜索? 因为它很好打,有些类似迭代的时候加的最优化剪枝 [因为这个最优化剪枝其实就是你算的估价函数了...] BZOJ 1085 骑士精 ...
- jquery cdn加速点
新浪jquery cdn加速点: <script src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.js">< ...