Rotate Image

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?

最简单的想法,两重循环。

第一重循环是对于矩阵的层次(从外到内)

第二重循环是对于每一层次的四元素轮换

举例:

1 2 3

4 5 6

7 8 9

最外层:

1-->3-->9-->7

2-->6-->8-->4

最内层:

5

class Solution {
public:void rotate(vector<vector<int> > &matrix) {if(matrix.empty())return;int n = matrix.size();int temp;for(int i = 0; i < (n+1)/2; i ++){for(int j = i; j < n-1-i; j ++){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;}}}
};

还有一种技巧性比较强的解法。

顺时针旋转90度可以分解为两步:

1、上下颠倒reverse

2、转置transpose

举例:

1 2 3

4 5 6

7 8 9

reverse之后

7 8 9

4 5 6

1 2 3

transpose之后

7 4 1

8 5 2

9 6 3

class Solution {
public:void rotate(vector<vector<int> > &matrix) {if(matrix.empty() || matrix[0].empty())return;reverse(matrix.begin(), matrix.end());int m = matrix.size();int n = matrix[0].size();for(int i = 0; i < m; i ++){for(int j = i+1; j < n; j ++){swap(matrix[i][j], matrix[j][i]);}}}
};

转载于:https://www.cnblogs.com/ganganloveu/p/3758105.html

【LeetCode】48. Rotate Image (2 solutions)相关推荐

  1. 【10】48. Rotate Image

    48. Rotate Image Total Accepted: 96625 Total Submissions: 259249 Difficulty: Medium Contributors: Ad ...

  2. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  3. 【LeetCode】69. Sqrt(x) (2 solutions)

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...

  4. 【LeetCode】剑指 Offer 48. 最长不含重复字符的子字符串

    [LeetCode]剑指 Offer 48. 最长不含重复字符的子字符串 文章目录 [LeetCode]剑指 Offer 48. 最长不含重复字符的子字符串 package offer;import ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  7. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  8. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  9. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

最新文章

  1. Sizeof与Strlen的区别与联系(转)
  2. 抽奖算法-指定概率的随机
  3. 隐函数画图with R
  4. QQ2009任务栏的QQ图标怎么隐藏
  5. Android新增Activity,并实现多Activity之间的切换
  6. dnspod-sr的安装和使用
  7. 优化技巧与理论(part1)
  8. Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)
  9. mysql学习(2)索引的本质
  10. 万物互联下的碎片化怎么破?UINO优锘推出物联网产业元宇宙“物联森友会”
  11. mysql ip比较大小_MySQL优化/面试,看这一篇就够了
  12. Objectove-c单例模式
  13. EDUCoder编程练习题解(一维数组和二维数组)
  14. 计算机知识课程简单课件,计算机基础知识实用课件.ppt
  15. power query时间函数(思维导图)
  16. python自制一款职位分析器,一键生成岗位分析报告
  17. 【枚举与countDownLatch的应用】
  18. mbedtls | 09 - 数字签名算法的配置与使用(RSA数字签名算法、ECDSA数字签名算法)
  19. #名词区别篇:px、em、rem、rpx、vh、vw、vmin 、vmax、%用法 与 区别
  20. 互联网公司技术总监工作内容

热门文章

  1. 有什么推荐的计算机视觉项目?来自微软亚研院的清单
  2. 游戏建模成熟期:在这个阶段,技术已经比较成熟了,可以独挡一面
  3. 这个GitHub 1400星的Git魔法书火了,斯坦福校友出品
  4. 经验 | 一目了然,用动图展示 10 大 Git 命令
  5. 这就是数据分析之数据可视化基础概念及工具
  6. ubuntu中mysql怎么退出命令_ubuntu的Linux下安装MySQL
  7. java 模拟栈底层用数组_java用数组模拟栈
  8. tcp假连接_ESTABLISHED状态的连接收到 SYN 会回复什么?
  9. android版 点击下载,自动点击器最新版
  10. maven mirror 优先级