题目

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?

题解

这道题就是考察很直白的旋转坐标。要in place的。画个图自己算算就出来了。

代码如下:

 1  /*   public void rotate(int[][] matrix) {
 2         int m = matrix.length;
 3         int n = matrix[0].length;
 4         
 5         int[][] result = new int[m][n];
 6         
 7         for(int i = 0; i<m; i++){
 8             for(int j = 0; j<n; j++){
 9                 result[j][m-1-i] = matrix[i][j];
             }
         }
         
         for(int i=0;i<m;i++){
             for(int j=0; j<n; j++){
                  matrix[i][j] = result[i][j];
             }
         }
     }
     */
     
     //in place
     public void rotate(int[][] matrix) {
     int n = matrix.length;
     for (int i = 0; i < n / 2; i++) {
         for (int j = 0; j < Math.ceil(((double) n) / 2.); j++) {
             int temp = 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] = temp;
         }
     }

Rotate Image leetcode java的更多相关文章

  1. Rotate List leetcode java

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...

  2. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  3. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  4. LeetCode算法题-Rotate Array(Java实现)

    这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...

  5. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  6. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  7. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  8. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  9. 48. Rotate Image - LeetCode

    Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...

随机推荐

  1. 子域名枚举工具Sublist3r

    子域名枚举工具Sublist3r   通过搜集子域名信息,可以找到目标的关联网站,找寻相应的漏洞.Kali Linux提供一款基于OSINT的枚举工具Sublist3r.该工具会搜索多个数据来源,如G ...

  2. 斯坦纳树 [bzoj2595][wc2008]游览计划 题解

    话说挺早就写过斯坦纳树了,不过当时没怎么总结,也不是很理解……现在来个小结吧~ 斯坦纳树就是包含给定点的最小生成树(个人理解权值应当为正). 一般来讲,给定点的数目应该很小吧...于是我们可以用状压D ...

  3. HDU5420 : Victor and Proposition

    以深度建立线段树,线段树父亲节点向儿子节点连边,然后用线段树合并可以得到任何一个点子树的线段树,只需向对应节点的线段树中的$O(\log n)$个点连边即可.为了保证连边关系不发生混乱,线段树需要进行 ...

  4. 通过Windows Compatibility Pack补充.net core中缺失的api

    把项目往.net core上迁移的时候,一个最大的问题就是和.net framework相比,有一部分api缺失.它主要分为两类: Windows 独有的api,如注册表 未完成的功能,如System ...

  5. ARM JTAG 20P to Cortex JTAG 10P

  6. CRC 概述

    Acquired from: ftp.adelaide.edu.au:/pub/rocksoft/crc_v3.txt or ftp://ftp.rocksoft.com/papers/crc_v3. ...

  7. Git 修复 bug 切换分支时,如何保存修改过的代码(即如何保存现场)?

    工作除了开发最新的版本之外还要对原来的版本做例行的维护,修修补补.于是有了在两个分支之间游走切换的问题,最新改版的代码在分支 new 上,旧版本的代码在分支 old 上,我在 new 上开发了一半,忽 ...

  8. GUN WINDOW 工具

    GNU utilities for Win32 CoreUtils for Windows 或者 完整的 package dd for windows Unix ports - WHICH, TEE ...

  9. C++11 function

    #include <iostream> #include <functional> #include <vector> using namespace std; / ...

  10. delphi Image处理

    procedure ImageDrawText(ATextEdo: IGGCCADTextEDO); var oImageBitmap: TBitmap; x1,x2,y1,y2: double; b ...