【Leet Code】48. Rotate Image---Medium

原创 2015年11月18日 14:52:45

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?

思路:

该题目最大的难点就是理清二维数组的转换。对二维数组每圈每圈的处理(或者说一层一层的转)。理清数组位置。

代码实现:

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        if(n == 0) return;
        for(int h = 0; h < (n>>1); ++h) 
        {
            for(int i = h; i < n-1-h; ++i) 
            {
                int temp = matrix[h][i];
                matrix[h][i] = matrix[n-1-i][h];
                matrix[n-1-i][h] = matrix[n-1-h][n-1-i];
                matrix[n-1-h][n-1-i] = matrix[i][n-1-h];
                matrix[i][n-1-h] = temp;
            }
        }
    }
};


[leet code] Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise)....

Leet Code OJ 15. 3Sum[Difficulty: Medium]

Leet Code OJ 15. 3Sum[Difficulty: Medium]Given an array S of n integers, are there elements a, b, c ...

Leet Code OJ 15. 3Sum[Difficulty: Medium]

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a...

【Leet Code】147. Insertion Sort List---Medium

Sort a linkedlist using insertion sort. 思路: 插入排序主要有直接插入排序和希尔插入排序, 1) 直接插入排序 把一个记录插入到一个已经排好序的有序表中,从而得...
  • lili616
  • lili616
  • 2015年09月15日 16:08
  • 204

Leet Code OJ 482. License Key Formatting [Difficulty: Medium]

给定一个非空字符串S,代表一个软件授权秘钥,我们需要格式化这个秘钥。这个字符串由字母、数字(a-z and/or A-Z and/or 0-9)和中划线(-)组成。中划线把字符串S分为几个组(例如有M...

Leet Code OJ 91. Decode Ways [Difficulty: Medium]

题目: A message containing letters from A-Z is being encoded to numbers using the following mapping:'...

【Leet Code】148. Sort List---Medium

Sort a linkedlist in O(n log n) time using constant space complexity. 思路: 借助2-路归并排序实现。 基本思想: 归并排序(M...
  • lili616
  • lili616
  • 2015年09月15日 16:13
  • 214

Leet Code OJ 338. Counting Bits [Difficulty: Medium]

题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate...

leetcode解题之48. Rotate Image java版(旋转图像)

leetcode解题之48. Rotate Image java版(旋转图像)

LeetCode 第 48 题(Rotate Image)

LeetCode 第 48 题(Rotate Image) You are given an n x n 2D matrix representing an image. Rotate th...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Leet Code】48. Rotate Image---Medium
举报原因:
原因补充:

(最多只允许输入30个字)