当前位置:网站首页>Leetcode48. 旋转图像
Leetcode48. 旋转图像
2022-07-16 13:44:00 【Java全栈研发大联盟】
题目传送地址; https://leetcode.cn/problems/rotate-image/

代码如下:
public static void rotate(int[][] matrix) {
int width = matrix.length;
while (width > 0) {
Queue queue1 = pushDataToQueue(width, matrix);
fillDataToMatrix(width, queue1, matrix);
width = width - 2;
}
}
public static Queue pushDataToQueue(int width, int[][] matrix) {
int leftTopRowIndex = (matrix.length - width) / 2; //左上角的行坐标
int leftTopColIndex = (matrix.length - width) / 2; //左上角的列坐标
int rightBottomRowIndex = (matrix.length + width) / 2-1;//右下角的行坐标
int rightBottomColIndex = (matrix.length + width) / 2-1;//右下角的列坐标
int count = (width - 1) * 4; //一圈的元素数量
Queue<Integer> queue = new LinkedList<>();
int rowIndex = leftTopRowIndex, colIndex = leftTopColIndex;
while (count > 0) {
queue.add(matrix[rowIndex][colIndex]);
count--;
//从左到右
if (rowIndex == leftTopRowIndex && colIndex != rightBottomColIndex) {
colIndex++;
continue;
}
//从上到下
if (colIndex == rightBottomColIndex && rowIndex != rightBottomRowIndex) {
rowIndex++;
continue;
}
//从右往左
if (rowIndex == rightBottomRowIndex && colIndex != leftTopColIndex) {
colIndex--;
continue;
}
//从下往上
if (colIndex == leftTopColIndex && rowIndex != leftTopRowIndex) {
rowIndex--;
}
}
return queue;
}
public static void fillDataToMatrix(int width, Queue<Integer> queue, int[][] matrix) {
int rightTopRowIndex = (matrix.length - width) / 2; //右上角的行坐标
int rightTopColIndex = (matrix.length +width) / 2-1;// 右上角的列坐标
int leftBottomRowIndex = (matrix.length + width) / 2-1;// 左下角的行坐标
int leftBottomColIndex = (matrix.length - width) / 2; //左下角的列坐标
int rowIndex = rightTopRowIndex, colIndex = rightTopColIndex;
while (!queue.isEmpty()) {
int val = queue.poll();
matrix[rowIndex][colIndex] = val;
//从上到下
if (colIndex == rightTopColIndex && rowIndex != leftBottomRowIndex) {
rowIndex++;
continue;
}
//从右往左
if (rowIndex == leftBottomRowIndex && colIndex != leftBottomColIndex) {
colIndex--;
continue;
}
//从下往上
if (colIndex == leftBottomColIndex && rowIndex != rightTopRowIndex) {
rowIndex--;
continue;
}
//从左到右
if (rowIndex == rightTopRowIndex && colIndex != rightTopColIndex) {
colIndex++;
}
}
}
边栏推荐
- (Note)七彩虹30系列显卡——《一键超频》按键
- What should I do if I can't see any tiles on SAP Fiori launchpad?
- 華為影像XMAGE:求盡世間像,終見菩提心
- 常见的网路设备和网络参考模型,以及常见的网络层协议及数据通信过程
- LeetCode每日一题(947. Most Stones Removed with Same Row or Column)
- Pay equal attention to quality and efficiency, test left shift power block storage technology research and development
- log4j日志配置
- Data visualization: plotting pie chart with Matplotlib
- 质量与效率并重,测试左移助力块存储技术研发
- Native input['file'] uploads the same file, and onchange problem records will not be triggered repeatedly
猜你喜欢

找不到 cl.exe 解决办法

勤于奋闲聊

It's decided. There are 93 open source tasks in 6 fields. Alibaba open source tutor will take you to participate in the open source summer 2022 of the Chinese Academy of Sciences

26个顶级开源项目,87个开放任务,阿里巴巴编程之夏2022学生报名通道开启

S7-200SMART案例分析——运动控制编程(二)

Xray安装使用

module ‘sklearn. datasets‘ has no attribute ‘fetch_ mldata‘

PNAS | 南农张瑞福组揭示了微生物肥料功能菌根际趋化的信号识别新机制

NoSQLAttack工具安装与使用问题解决

Touche pussy cat
随机推荐
What should I do if I can't see any tiles on SAP Fiori launchpad?
才22岁!这位“00后”博士拟任职985高校!
Huawei image xmage: seek all the images in the world, and finally see the Bodhi Heart
一次有趣(想打断同事腿)的键盘BUG调试经历
云原生:Docker 实践经验(四)docker上部署 redis 三主三从集群
Package C language files into exe executable programs
网络爬虫实现发送短信验证码
UOS installation MariaDB
What is packet loss and why
Simple encapsulation of native fetch requests
阿里达摩院TableQA技术让表格说话
Digital pyramid (PTA)
[autosar-dem] - 2.4-app SWC how to read mil lamp status and configure mil lamp
JD finance, are you bad, or are you cutting too much??
监听拖拽事件,第一次拖拽得不到上传的文件内容,第二次以后就能正常得到上传的文件内容
PICASSO,一个高效的搜推广稀疏训练解决方案
2022年最新天津建筑安全员模拟题库及答案
sqlmap语法介绍
数据可视化之matplotlib绘制饼状图
ASP.NET印刷行业印务管理系统,源码免费分享