【leetcode】48. Rotate Image
来源:互联网 发布:淘宝消字灵是真的么 编辑:程序博客网 时间:2024/05/29 08:20
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).(将二维数组顺时针翻转90度)
Follow up:
Could you do this in-place?
Subscribe to see which companies asked this question
The idea was firstly transpose the matrix and then flip it symmetrically.(先转置,再水平翻转) For instance,
1 2 3 4 5 67 8 9
after transpose, it will be swap(matrix[i][j], matrix[j][i])
1 4 72 5 83 6 9
Then flip the matrix horizontally. (swap(matrix[i][j], matrix[i][matrix.length-1-j])
7 4 18 5 29 6 3
数组名.length指示数组的行数。数组名[行下标] .length指示该行中的元素个数。
public class Solution { public void rotate(int[][] matrix) { for(int i = 0; i<matrix.length; i++){ for(int j = i; j<matrix[0].length; j++){ int temp = 0; temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for(int i =0 ; i<matrix.length; i++){ for(int j = 0; j<matrix.length/2; j++){ int temp = 0; temp = matrix[i][j]; matrix[i][j] = matrix[i][matrix.length-1-j]; matrix[i][matrix.length-1-j] = temp; } } }}
同理 若将二维数组逆时针翻转90度,则先将数组转置,再将数组在垂直方向翻转。 0 0
- [LeetCode]48.Rotate Image
- LeetCode 48. Rotate Image
- LeetCode 48.Rotate Image
- LeetCode --- 48. Rotate Image
- [Leetcode] 48. Rotate Image
- [leetcode] 48. Rotate Image
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [LeetCode]48. Rotate Image
- leetcode 48. Rotate Image
- Leetcode 48. Rotate Image
- LeetCode 48. Rotate Image
- LeetCode *** 48. Rotate Image
- [LeetCode]48. Rotate Image
- LeetCode 48. Rotate Image
- LeetCode-48.Rotate Image
- [LeetCode] 48. Rotate Image
- LeetCode 48. Rotate Image
- JavaScript高级程序设计6--BOM
- 关于文件
- javaweb基础(JSP)
- Java并发之不可思议的死循环
- ARC 065C DayDream Trie+暴力
- 【leetcode】48. Rotate Image
- Java基础Day06笔记
- WebService学习笔记(二) - XML的dtd和schema
- 鸟人的Android揭秘(1)——源代码构成
- |洛谷|搜索|NOIP2008|P1149 火柴棒等式
- 第三节 利用curl获取access_token和微信服务器地址
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- 使用LinkedList存储一副扑克牌(除开大小王),实现洗牌功能。
- 微痕迹捐赠说明