Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
awesome-algorithm
提交
3522a0a2
A
awesome-algorithm
项目概览
OpenDocCN
/
awesome-algorithm
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
awesome-algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
3522a0a2
编写于
5月 05, 2019
作者:
K
Keqi Huang
提交者:
GitHub
5月 05, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #261 from Williamren97/patch-3
Create 0048._Rotate_Image.md
上级
69050b88
80c16b5e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
71 addition
and
0 deletion
+71
-0
docs/Leetcode_Solutions/C++/0048._Rotate_Image.md
docs/Leetcode_Solutions/C++/0048._Rotate_Image.md
+71
-0
未找到文件。
docs/Leetcode_Solutions/C++/0048._Rotate_Image.md
0 → 100644
浏览文件 @
3522a0a2
# 49. Group Anagrams
**<font color=red>难度: Medium</font>**
## 刷题内容
> 原题连接
*
https://leetcode.com/problems/rotate-image/submissions/
> 内容描述
```
给你一个矩阵,让你顺时针旋转90° 思路: 不讨论复制一个数组然后在旋转的方法,因为太简单了。 下面的方法都是in-place的:。
示例:
输入:
[
[1,2,3],
[4,5,6],
[7,8,9]
],
输出:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
说明:
必须直接修改输入的2D矩阵。
不要分配另一个2D矩阵并进行旋转。
```
## 解题方案
> 思路 1
```
直接设置top, bottom, left, right四个变量,表示圈定当前要旋转的正方形范围,
然后按偏移计算位置,比如左上角的先和右上角交换,然后继续左上角和右下角交换这样即可.
```
```
cpp
class
Solution
{
public:
void
rotate
(
vector
<
vector
<
int
>>&
matrix
)
{
if
(
matrix
.
empty
())
return
;
int
top
=
0
,
bottom
=
matrix
.
size
()
-
1
;
int
left
=
0
,
right
=
matrix
[
0
].
size
()
-
1
;
for
(;
top
<
bottom
&&
left
<
right
;
++
top
,
++
left
,
--
bottom
,
--
right
){
for
(
int
i
=
left
;
i
<
right
;
++
i
){
int
dis
=
i
-
left
;
int
row
=
top
+
dis
;
int
col
=
right
;
swap
(
matrix
[
top
][
i
],
matrix
[
row
][
col
]);
row
=
bottom
;
col
=
right
-
dis
;
swap
(
matrix
[
top
][
i
],
matrix
[
row
][
col
]);
row
=
bottom
-
dis
;
col
=
left
;
swap
(
matrix
[
top
][
i
],
matrix
[
row
][
col
]);
}
}
}
};
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录