给定一个 n × n 的二维矩阵表示一个图像。

将图像顺时针旋转 90 度。

说明:

你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。

示例 1:

给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ]

示例 2:

给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], 原地旋转输入矩阵,使其变为: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ]

顺时针旋转矩阵的步骤

1.把矩阵上下翻转。比如[1,2][3,4] -> [3, 4][1, 2]

2.将矩阵沿着从左上角到右下角的对角线进行翻转

如果是逆时针

则将1,2步倒着操作

如果记不住顺序,可以试着用二维数组模拟一下

class Solution {
public:
void rotate(vector<vector<int> >& matrix)
{
int r = matrix.size();
if(r == 0)
return;
int c = matrix[0].size();
for(int i = 0; i < r / 2; i++)
{
for(int j = 0; j < c; j++)
{
swap(matrix[i][j], matrix[r - 1 - i][j]);
}
}
for(int i = 0; i < r; i++)
{
for(int j = i + 1; j < c; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
}
};

Leetcode48. Rotate Image旋转图像的更多相关文章

  1. 048 Rotate Image 旋转图像

    给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [  [1,2,3],  [4 ...

  2. [LeetCode] Rotate Image 旋转图像

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

  3. LeetCode48 Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  4. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

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

  5. [leetcode]48. Rotate Image旋转图像

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

  6. Rotate Image 旋转图像

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

  7. [LeetCode] 48. Rotate Image 旋转图像

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

  8. [CareerCup] 1.6 Rotate Image 翻转图像

    1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...

  9. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

随机推荐

  1. 项目接入即时聊天客服系统(环信系统)PHP后端操作

    环信工作原理: 一.由于环信没有直接的接口来主动调取本项目中的用户数据,所有用户信息必须在环信服务器上注册对应信息成为环信的用户:(这样才能当用户进入聊天时显示其基本信息,如:名称.昵称.电话.邮箱等 ...

  2. tab切换 -- vue

    效果: html: // 按钮<div class="orderTab clearfix" @click="toggle()"> <div c ...

  3. MySQL-Utilities:mysqldbcompare及跳过复制错误

    mysqldbcompare也是MySQL-Utilities工具集的一个脚本.mysqldbcompare从两个数据库比较对象和数据的不同.数据库中的对象包括:表.视图.触发器.存储过程.函数和事件 ...

  4. jquery移除元素某个属性

    removeAttr() 方法从被选元素中移除属性. 例如:$("p").removeAttr("style");

  5. Leetcode300. Longest Increasing Subsequence最长上升子序列

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  6. 再不懂时序就 OUT 啦!,DBengine 排名第一时序数据库,阿里云数据库 InfluxDB 正式商业化!

    云数据库 InfluxDB® 版介绍 阿里云数据库 InfluxDB® 版已于近日正式启动商业化 . 云数据库 InfluxDB® 是基于当前最流行的开源数据库 InfluxDB 提供的在线数据库服务 ...

  7. 微信小程序——页面滑动事件

    wxml: <view id="id" class = "ball" bindtap = "handletap" bindtouchs ...

  8. 路由的配置,侧边栏类名与url的结合运用

    var url_array = document.location.pathname.split("/"); s1 = url_array[1]; s2 = url_array[2 ...

  9. PHP declare 之 strict_types=1

    PHP中申明 declare(strict_types=1)的作用:  strict_types=1 及开启严格模式.默认是弱类型校验.具体严格模式和普通模式的区别见下面代码. code1: < ...

  10. RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景?

    RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? - 知乎 https://www ...