LeetCode C++ 48. Rotate Image【Array】中等
发布日期:2021-07-01 02:58:15
浏览次数:3
分类:技术文章
本文共 1159 字,大约阅读时间需要 3 分钟。
You are given an n x n 2D matrix
representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image , which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Example 3:
Input: matrix = [[1]]Output: [[1]]
Example 4:
Input: matrix = [[1,2],[3,4]]Output: [[3,1],[4,2]]
Constraints:
matrix.length == n
matrix[i].length == n
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
题意:给定一个 n × n
的二维矩阵表示一个图像。将图像顺时针旋转 90
度。
解法1 额外空间
这种做法就不写了。
解法2 常量空间
class Solution { public: void rotate(vector>& matrix) { int n = matrix.size(); for (int i = 0; i < n; ++i) for (int j = 0; j < i; ++j) swap(matrix[i][j], matrix[j][i]); for (int i = 0; i < n; ++i) reverse(matrix[i].begin(), matrix[i].end()); }};
运行效率如下:
执行用时:4 ms, 在所有 C++ 提交中击败了64.58% 的用户内存消耗:7.1 MB, 在所有 C++ 提交中击败了69.34% 的用户
转载地址:https://memcpy0.blog.csdn.net/article/details/111399610 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月09日 04时05分48秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
结合一道面试题 看c语言运算符的执行顺序
2019-05-03
Objective-C的内省方法介绍
2019-05-03
Objective-C 内存管理 看这个就够啦
2019-05-03
IOS开发--微信支付
2019-05-03
iOS 微信支付 实用教程
2019-05-03
UIViewController的基本概念与生命周期
2019-05-03
最新方法制作自己的cocoapods开源框架的详细步骤
2019-05-03
Getting start with OCMock in you unit test
2019-05-03
李洪强和你一起学习前端之(1)Html基础
2019-05-03
李洪强iOS经典面试题142-第三方框架及其管理
2019-05-03
李洪强经典面试题38
2019-05-03
我们必须自学
2019-05-03
iOS应用内付费(IAP)开发步骤列表
2019-05-03
iOS-TextField知多少
2019-05-03
用javascript协助导入图片
2019-05-03
白话 Ruby 与 DSL 以及在 iOS 开发中的运用
2019-05-03
深入理解哈希表
2019-05-03
获取任意线程调用栈的那些事
2019-05-03
结合 category 工作原理分析 OC2.0 中的 runtime
2019-05-03
主线程中也不绝对安全的 UI 操作
2019-05-03