题目

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. Mysql update case

    UPDATE table SET total = CASE WHEN total = '1' THEN total- 1 ELSE total = '2' END WHERE id = 17

  2. C#中执行Dos命令

    //dosCommand Dos命令语句 public string Execute(string dosCommand) { ); } /// <summary> /// 执行DOS命令 ...

  3. NSNotificationCenter消息注册与撤销

    苹果的消息机制是个非常好用的东西,当需要在类的各个实例之间传递消息或者写一些事件驱动的程序时,绝对是个不错的工具.但是使用时一不小心就会造成引用已经被dealloc的对象的错误,引起程序崩溃.于是,在 ...

  4. 【python】python安装步骤

    1.官网下载python 官网地址:https://www.python.org/getit/ 2.下载完成后点击安装 勾选Add python to PATH 是可以自己去配置环境变量的 注意:这里 ...

  5. spring boot对输入的字符串进行html转码

    可以使用HtmlUtils这个类进行操作.具体的可以参考API,或者点出来看.

  6. 寂静之地百度云在线观看迅雷下载A Quiet Place高清BT下载

      原名:A Quiet Place 地区:美国 语言:英语 / 美国手语 首播:2018-05-18(中国大陆) / 2018-03-09(西南偏南电影节) / 2018-04-06(美国) 电视台 ...

  7. 罪恶黑名单第四季/全集The Blacklist迅雷下载

    英文全名The Blacklist,第1季(2016)NBC.本季看点:<罪恶黑名单>我们知道:剧情紧接第三季结尾,每个人——Liz,Red以及特别行动组的其他人——似乎都有许多故事可说: ...

  8. 血族第四季/全集The Strain迅雷下载

    当第四季开始时,故事时间已经过去九个月.世界陷入黑暗,斯特里高伊吸血鬼控制了一切.第三季结尾的爆炸引发了一场全球核灾难,核冬天的到来令地表变得暗无天日,斯特里高伊获得解放.它们大白天也能出来活动,帮助 ...

  9. 从源码角度一步一步来修改PreferenceActivity界面

         PreferenceActivity给我们封装好了一个数据存储对象,我们只需要在xml文件中写上控件即可完成简单的设置界面.但是系统提供的设置界面十分的简陋,要想做的好看必须要自己来进行修改 ...

  10. ImageView和onTouchListener实现,点击查看图片细节

    这是来自疯狂android讲义上的例子,其实没啥意思.就是用监听器获取到手指的坐标,然后根据这个坐标开始绘制一个图片.(这里的绘制方式有些问题,所以凑合看看吧.) 首先,还是布局文件(两个ImageV ...