Java实现 LeetCode_0048_RotateImage
package javaLeetCode.medium;
public class RotateImage_48 {
public static void main(String[] args) {
int[][] matrix = { {5, 1, 9,11}, { 2, 4, 8,10}, {13, 3, 6, 7},{15,14,12,16} };
rotate_2(matrix);
}// end main()
/**
* Conventional idea.<br>
* 1. It's rotating diagonally<br>
* 2. 2. Rotating column.<br>
* 3. Print the array of matrix.<br>
* */
/*
* Test Data: Example 1: Given input matrix = { {1,2,3}, {4,5,6}, {7,8,9} },
* rotate the input matrix in-place such that it becomes: { {7,4,1},{8,5,2},{9,6,3} }
*
* Example 2:
* Given input matrix = { {5, 1, 9,11}, { 2, 4, 8,10}, {13, 3, 6, 7},{15,14,12,16} },
* rotate the input matrix in-place such that it becomes: { {15,13, 2, 5}, {14,3, 4, 1}, {12, 6, 8, 9}, {16, 7,10,11} }
*/
public static void rotate_1(int[][] matrix) {
if(matrix==null) {
return;
}//end for
//=====================================================
// 1. It's rotating diagonally
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (i < j) {
matrix[i][j] = matrix[i][j] ^ matrix[j][i];
matrix[j][i] = matrix[i][j] ^ matrix[j][i];
matrix[i][j] = matrix[i][j] ^ matrix[j][i];
} else {
continue;
} // end if
} // end for
} // end for
//2. Rotating column.
for(int i=0;i<matrix.length;i++) {
int length = matrix[i].length;
for(int j=0;j<length/2;j++) {
matrix[i][j] = matrix[i][j] ^ matrix[i][length-j-1] ;
matrix[i][length-j-1] = matrix[i][j] ^ matrix[i][length-j-1] ;
matrix[i][j] = matrix[i][j] ^ matrix[i][length-j-1] ;
}//end for
}//end for
//=====================================================
//3. Print the array of matrix.
for (int x[] : matrix) {
for (int y : x) {
System.out.print(y + " ");
} // end for
System.out.println();
} // end for
}// end rotate()
/**
*
* */
public static void rotate_2(int[][] matrix) {
if(matrix==null) {
return;
}//end for
//=====================================================
int n = matrix.length;
for (int i = 0; i < n / 2; ++i) {
for (int j = i; j < n - 1 - i; ++j) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = tmp;
}//end for
}//end for
//=====================================================
//Print the array of matrix.
for (int x[] : matrix) {
for (int y : x) {
System.out.print(y + " ");
} // end for
System.out.println();
} // end for
}// end rotate()
}// end RotateImage_48
Java实现 LeetCode_0048_RotateImage的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- CentOS8 右键打开后没有终端
最近研究CentOS8 发现右键打开后没有终端这一项: 1.经过查询发现是没有安装一个包 2.使用命令进行安装并重启: [root@base ~]# yum -y install nautilus- ...
- spring mvc json返回防止乱码
乱码问题 乱码一直是编程的常见问题,spring mvc 返回json数据时可能导致乱码,需要在controller中添加如下代码: @RequestMapping("/test" ...
- 1、Fiddler基础
1.抓取https请求 前言 fiddler是一个很好的抓包工具,默认是抓http请求的,对于pc上的https请求,会提示网页不安全,这时候需要在浏览器上安装证书. 一.网页不安全 1.用fiddl ...
- 网鼎杯2020青龙组writeup-web
本文首发于Leon的Blog,如需转载请注明原创地址并联系作者 AreUSerialz 开题即送源码: <?php include("flag.php"); highligh ...
- Redis安装启动(linux系统)
Redis简介 redis是以kv存储的nosql非关系型数据库 sql支持事务性,nosql不支持 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间 ...
- ORA-12519,TNS:no appropriate service handler found的问题 超过连接数
http://www.2cto.com/database/201205/133542.html ORA-12519,TNS:no appropriate service handler found的问 ...
- 实验十四 Swing图形界面组件
实验十四 Swing图形界面组件 实验时间 20178-11-29 1.实验目的与要求 (1) 掌握GUI布局管理器用法: (2) 掌握各类Java Swing组件用途及常用API: 2.实验内容和 ...
- SPOJ-PGCD Primes in GCD Table
题目链接:https://vjudge.net/problem/SPOJ-PGCD 题目大意: 给定 \(N\) 和 \(M\),求满足 \((1 \le x \le N), (1 \le y \le ...
- 一个茴字有三种写法——吐槽C#9.0的Records
最近是微软开了Build 2020大会,由于疫情原因,改成了在线举行,Build大会上,C#公布9.0版本. 我个人对于C#的更新向来都是喜闻乐见,乐于接受的,对于博客园上某些人天天嘲讽C#只会增加语 ...
- SQL——AUTO INCREMENT(字段自增)
AUTO INCREMENT -- 在新记录插入表中时生成一个唯一的数字.插入表数据时,该字段不需规定值. 在每次插入新记录时,自动地创建主键字段的值.在表中创建一个 auto-incremen ...