题目

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. iOS 七牛多张图片上传

    -(void)uploadImages:(NSArray *)images atIndex:(NSInteger)index token:(NSString *)token uploadManager ...

  2. HQL的内连接查询

    /** * HQL的内连接查询 * String hql="from Customer c inner join fetch c.linkmans"; */ @Test publi ...

  3. j.u.c系列(11)---之并发工具类:Exchanger

    写在前面 前面三篇博客分别介绍了CyclicBarrier.CountDownLatch.Semaphore,现在介绍并发工具类中的最后一个Exchange.Exchange是最简单的也是最复杂的,简 ...

  4. Tomcat无法启动8005端口,提示:java.net.ConnectException: 拒绝连接 (Connection refused)

    修改$JAVA_HOME/jre/lib/security/Java.security 文件中 securerandom.source 配置项: 将 securerandom.source=file: ...

  5. API网关的设计思路及落地 IT大咖说 - 大咖干货,不再错过

    API网关的设计思路及落地 IT大咖说 - 大咖干货,不再错过   http://www.itdks.com/dakashuo/new/dakalive/detail/1407

  6. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. nRF51 DK : nRF51822 Development Kit for Bluetooth Smart, ANT and 2.4GHz applications.

    KEY FEATURES • Affordable, Rapid prototyping and development solution for nRF51 Series SoCs • Kit su ...

  8. ST推出新软件STM32Cube ,让STM32微控制器应用设计变得更容易、更快、更好用

    功能强大的STM32Cube 新软件平台由设计工具.中间件和硬件抽象层组成,让客户能够集中精力创新 意法半导体(STMicroelectronics,简称ST)针对STM32微控制器推出一套免费的功能 ...

  9. c# SerialPort会出现“已关闭 Safe handle”的错误

    c# SerialPort使用时出现“已关闭 Safe handle”的错误我在开发SerialPort程序时出现了一个问题,在一段特殊的扫描代码的时候会出现“已关闭 Safe handle”的错误, ...

  10. UVA 303 Pipe

    点击打开链接 题意: 求光线能达到的最大横坐标 注意光线可以和管道重合 也可以经过转折点 解法: 枚举每种光线是否能通过每个转折点的截面(线段)即可 //大白p263 #include <cma ...