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 ...
随机推荐
- JAVA实现拼手气红包算法
实现拼手气红包算法,有以下几个需要注意的地方: 抢红包的期望收益应与先后顺序无关 保证每个用户至少能抢到一个预设的最小金额,人民币红包设置的最小金额一般是0.01元,如果需要发其他货币类型的红包,比如 ...
- python 基础知识1
一.编译型与解释性区别: 编译型:一次性将全部的代码编译成二进制文件.(如:C.C++) 优点:运行效率高 缺点:开发速度慢,不能跨平台. 解释型:当程序运行时,从上至下一行一行的解释成二进制.(如p ...
- ipad4密码忘记锁定了如何破解
ipad4更新后被要求输入密码,但很长一段时间后忘记了,一直想不起来,也没有忘记密码的选项,以下是简单的破解方法. 注意:没有备份的资料是要被清空的 一.windows10系统,安装iTunes安装 ...
- 对background: url("~assets/img/common/collect.svg") 0 0/14px 14px 的理解
需求:给收藏数字前面通过::before伪元素添加图标 相关代码: .goods-info .collect { position: relative; } .goods-info .collect: ...
- .net core 3.1 使用nlog记录日志 NLog.Web.AspNetCore
背景 .net core 中已经集成了log的方法, 但是只能控制台输出不能写入文件等等. 常见第三方的的日志工具包括log4net, nlog等等, 本文介绍nlog 一. 引用程序集, nuget ...
- vue的slot
1.明确一点:分发内容是在父作用域内编译: 2.slot作为备用内容的条件:宿主元素为空且父元素没有要分发的内容. 3.具名slot:<slot name="XXX"> ...
- Centos7安装jupyter notebook
安装python3 查看当前python版本 [root@iz1i4qd6oynml0z /]# python -V Python 2.7.5 安装python3以及检查python3的版本 yum ...
- AJAX一
一.http协议 1.URL http://www.baidu.com/md/index.html 结构:协议+主机名称+目录结构+文件名称 URL完整的结构 <scheme>://< ...
- HTML标签和属性三
八.列表 1.列表的作用 让数据有条理的显示,在数据之前添加标识 但是现在页面布局,经常会使用到无序列表 2.列表的组成 ①有序列表 <ol> <li></li> ...
- MySQL事务隔离之MVCC版本控制
MVCC简介 MVCC是一种多版本并发控制机制. MVCC是为了解决什么问题? 大多数的MYSQL事务型存储引擎,如,InnoDB,Falcon以及PBXT都不使用一种简单的行锁机制.事实上,他们都和 ...