[leetcode]48RotateImage二维数组翻转
import java.util.Arrays; /**
* 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? 翻转二维数组,思路是把数组分成length/2层,每层分别翻转,左边的到上边,下边的到左边,右边的到下边,上边的到右边
每层也有一个循环,设置一个中间量,这层的数一个一个交换,注意每层循环的次数(就是这层每条边的数量-1)
*/
public class Q48RotateImage {
public static void main(String[] args) {
int[][] matrix = new int[][]{{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
rotate(matrix);
for (int[] num :
matrix) {
System.out.println(Arrays.toString(num));
} }
public static void rotate(int[][] matrix) {
int temp;
int l = matrix.length;
for (int i = 0; i < l / 2; i++) {
for (int j = 0; j < l-1-(2*i); j++) {
temp = matrix[i][i+j];
matrix[i][i+j] = matrix[l-1-i-j][i];
matrix[l-1-i-j][i] = matrix[l-1-i][l-1-i-j];
matrix[l-1-i][l-1-i-j] = matrix[i+j][l-1-i];
matrix[i+j][l-1-i] = temp;
}
}
}
}
[leetcode]48RotateImage二维数组翻转的更多相关文章
- Arrays工具、二维数组以及LeetCode练习题
1 Arrays PS:Arrays位于java.util包下 int binarySearch(type[] a, type key); 使用二分法查询 key 元素在 a 数组中的索引,如果数组不 ...
- Excel 二维数组(数据块)旋转/翻转技巧
Excel 二维数组(数据块)旋转/翻转技巧 原创 2017-12-30 久石六 久石六 工作中遇到个问题,需要将Excel中的数据块或者说二维数组向右旋转90度,才能再加工处理.当然,不是旋转文本方 ...
- [leetcode]54. Spiral Matrix二维数组螺旋取数
import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...
- LeetCode二维数组中的查找
LeetCode 二维数组中的查找 题目描述 在一个 n*m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增.请完成一个搞笑的函数,输入这样的一个二维数组和一个整数,判断数 ...
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- [leetcode]304Range Sum Query 2D - Immutable动态规划计算二维数组中子数组的sum
303一维数组的升级版,方法就是用二维数组res存下从(0,0)到当前位置的sum,存的方法是动态规划,看着二维数组画圈比较好搞清楚其中的加减法 算子数组的sum的时候也是和存差不多的逻辑,就是某一部 ...
- 【LeetCode】剑指 Offer 04. 二维数组中的查找
二维数组查找:线性查找法 有二维数组: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, ...
- LeetCode - 二维数组及滚动数组
1. 二维数组及滚动数组总结 在二维数组num[i][j]中,每个元素都是一个数组.有时候,二维数组中的某些元素在整个运算过程中都需要用到:但是有的时候我们只需要用到前一个或者两个数组,此时我们便可以 ...
随机推荐
- 【服务总线 Azure Service Bus】Service Bus在使用预提取(prefetching)后出现Microsoft.Azure.ServiceBus.MessageLockLostException异常问题
问题描述 Service Bus接收端的日志中出现大量的MessageLockLostException异常.完整的错误消息为: Microsoft.Azure.ServiceBus.MessageL ...
- C语言中 EXIT_FAILURE和EXIT_SUCCESS
1.C语言中 宏EXIT_FAILURE和EXIT_SUCCESS定义在头文件stdlib.h中,是一个符号常量,定义如下: #define EXIT_FAILURE 1 #define EXIT_S ...
- day7(vue发送短信)
1.vue发送短信逻辑 前端函数如下,js方法代码无需更改,前端代码逻辑在components\common\lab_header.vue 只需要修改components\axios_api\http ...
- PyQt(Python+Qt)学习随笔:QListWidget查找项的findItems方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidget列表部件的findItems方法用于查找列表部件是否有满足条件的项,调用语法如 ...
- 记一次UE4源码编译和游戏项目打包过程
1.首先我们需要注册一个Epic账户,网址如下 http://api.unrealengine.com/CHN/GettingStarted/Installation/index.html#bookm ...
- flask注册蓝图报错
记录下这个我找了两天的坑... take no arguments() 这两天一直学习flask的时候,我把注册的蓝图,写成注册的form表单的 举个栗子 class TetsView(view.Me ...
- Python运算符可不只有加减乘除
数学里面的加减乘除,就是运算符,但是 Python 的运算符更多样,更复杂,分为算术运算符.比较运算符.赋值运算符.位运算符.逻辑运算符.成员运算符.身份运算符.为了更直观的看到运算符的使用,本文采用 ...
- go学习49天
写文件操作 func OpenFile(name string,flag int,perm FileMode) (file *File,err error)
- ❤空植发队 影评网站FilmReviewWeb-团队介绍
空植发队 团队展示&选题 团队展示 团队信息 队名: 空植发队 队员: 罗泉水(队长)3118005101 麦狄龙 3118005103 马志鹏 3118005102 陈鸿畅 3118 ...
- 落谷P3041 [USACO12JAN]Video Game G
题目链接 多模式匹配问题,先建 AC 自动机. 套路性的搞个 DP: \(f[i][j]\) 表示前 \(i\) 个字符,当前在 \(AC\) 自动机上的节点是 \(j\) 能匹配到的最多分. 初始化 ...