Rotate ImageMar 18 '124182 / 9471
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?
这题我第一次做时候,实现的非常复杂。人肉实现rotate。code很麻烦。
后来偶然间看到同学的做法,很妙:
rotate(M) = traspose(M)后,swap列
例如:
123
456
789
先transpose:
147
258
369
然后对调第1,3列
741
852
963
OK!
class Solution { public: void rotate(vector<vector<int> > &matrix) { int len = matrix.size(); if (len == 0) return ; for (int i = 0; i < len; i++) { for (int j = i; j < len; j++) { swap(matrix[i][j], matrix[j][i]); } } int i = 0, j = len - 1; while (i < j) { for (int k = 0; k < len; k++) { swap(matrix[k][i], matrix[k][j]); } i++, j--; } } };
相关推荐
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
leetcode 答案 LeetCode 该项目是 LeetCode 上的题解。 src/easy/路径下都是难度为 ..._48_RotateImage这个类名。运行效果如下图所示: 把这个类名复制一下,新建类的时候直接把类名粘贴过去就可以了。
leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 ...Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -> 2 26. Remove Duplicates from Sorted Array (E) 27. ...
RotateImage 153 - SpiralMatrix 顺时针旋转矩阵 90 度。 首先翻转矩阵,交换对称153是直接的 01/31/2020 75 - SortColors 167 - TwoSum2 DCP 75 是一个双指针问题,如果当前项为 0,则使用 p1 p2 指向开始和结束,...
旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/...
Rotate Image 344. Reverse String 414. Third Maximum Number 448. Find All Numbers Disappeared in an Array 66. Plus One 238. Product of Array Except Self 697. Degree of an Array 849. Maximize ...
java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: echo ...##leetcode ...[Rotate Image] () [Ugly Number] () [Ugly Number II] () [Repeated DNA Sequences] () [Lar
Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self ...