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的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  7. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  8. Java多线程基础学习(二)

    9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...

  9. Java多线程基础学习(一)

    1. 创建线程    1.1 通过构造函数:public Thread(Runnable target, String name){}  或:public Thread(Runnable target ...

随机推荐

  1. 1025 PAT Ranking (25分) 思路分析 +满分代码

    题目 Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of ...

  2. 死磕synchronized底层实现

    点赞再看,养成习惯,微信搜索[三太子敖丙]第一时间阅读. 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试完整考点.资料以及我的系列文章. 前言 ...

  3. HttpServletRequest与HttpServletResponse

    一. 简介:每当客户端给Web服务器发送一个http请求,web服务器就会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和respons ...

  4. mysql 5.7.22安装

    1.解压目录. 2.安装 3.重新设置密码 set password=password('123456'); FLUSH PRIVILEGES;

  5. Kubernetes实战 - 从零开始搭建微服务 1 - 使用kind构建一个单层架构Node/Express网络应用程序

    使用kind构建一个单层架构Node/Express网络应用程序 Kubernetes实战-从零开始搭建微服务 1 前言 准备写一个Kubernetes实战系列教程,毕竟cnblogs作为国内最早的技 ...

  6. 00003-aspose for java 生成水印刻章等,可转为word,pic,pdf

    对应java代码: package com.yoooya.ytp.utils.doc; import com.aspose.words.Document; import com.aspose.word ...

  7. String的正则函数

    String的正则函数: 查找敏感词: 4种情况 1. 查找一个固定的敏感词的位置i: var i=str.indexOf("敏感词",fromi) 在str中,从fromi位置开 ...

  8. 数据结构----栈stack

    栈的概念与数据结构 栈(有时称为“后进先出栈”)是一个元素的有序集合,其中添加移除新元素总发生在同一端.这一端通常称为“顶部”.与顶部对应的端称为“底部”.栈的底部很重要,因为在栈中靠近底部的元素是存 ...

  9. NPOI导入excel为datatable (xls xlsx xlsm)

    使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中 http://www.cnblogs.com/songrun/p/3547738.html NPOI 2.0教程 – 自动 ...

  10. Windows Terminal安装并美化

    介绍 Windows Teminal是一款新式.快速.高效.强大的终端应用程序,适用于命令行工具.命令提示符.PowerShell.WSL(Linux子系统)等等的Shell用户,主要功能包括多选项卡 ...