Skip to content

Commit 1def09a

Browse files
committed
feat: add solutions to lc problem: No.0048
No.0048.Rotate Image
1 parent c7a8d82 commit 1def09a

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

solution/0000-0099/0048.Rotate Image/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ class Solution {
8989
}
9090
```
9191

92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
void rotate(vector<vector<int>>& matrix) {
98+
99+
int n = matrix.size();
100+
if(n <= 1)return ;
101+
102+
//先做转置
103+
for(int i = 0 ; i < n ; i++){
104+
for(int j = i;j < n ;j++){
105+
swap(matrix[i][j],matrix[j][i]);
106+
}
107+
}
108+
109+
//再做水平互换
110+
for(int i = 0 ; i < n ; i++){
111+
for(int j = 0;j < n/2;j++){
112+
swap(matrix[i][j],matrix[i][n-1-j]);
113+
}
114+
}
115+
}
116+
};
117+
```
118+
92119
### **TypeScript**
93120
94121
```ts
@@ -109,6 +136,25 @@ function rotate(matrix: number[][]): void {
109136
}
110137
```
111138

139+
### **Rust**
140+
141+
```rust
142+
impl Solution {
143+
pub fn rotate(matrix: &mut Vec<Vec<i32>>) {
144+
let n = matrix.len();
145+
for i in 0..n / 2 {
146+
for j in i..n - i - 1 {
147+
let t = matrix[i][j];
148+
matrix[i][j] = matrix[n - j - 1][i];
149+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
150+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
151+
matrix[j][n - i - 1] = t;
152+
}
153+
}
154+
}
155+
}
156+
```
157+
112158
### **...**
113159

114160
```

solution/0000-0099/0048.Rotate Image/README_EN.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ class Solution {
7777
}
7878
```
7979

80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
void rotate(vector<vector<int>>& matrix) {
86+
87+
int n = matrix.size();
88+
if(n <= 1)return ;
89+
90+
for(int i = 0 ; i < n ; i++){
91+
for(int j = i;j < n ;j++){
92+
swap(matrix[i][j],matrix[j][i]);
93+
}
94+
}
95+
96+
for(int i = 0 ; i < n ; i++){
97+
for(int j = 0;j < n/2;j++){
98+
swap(matrix[i][j],matrix[i][n-1-j]);
99+
}
100+
}
101+
}
102+
};
103+
```
104+
80105
### **TypeScript**
81106

82107
```ts
@@ -97,6 +122,25 @@ function rotate(matrix: number[][]): void {
97122
}
98123
```
99124

125+
### **Rust**
126+
127+
```rust
128+
impl Solution {
129+
pub fn rotate(matrix: &mut Vec<Vec<i32>>) {
130+
let n = matrix.len();
131+
for i in 0..n / 2 {
132+
for j in i..n - i - 1 {
133+
let t = matrix[i][j];
134+
matrix[i][j] = matrix[n - j - 1][i];
135+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
136+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
137+
matrix[j][n - i - 1] = t;
138+
}
139+
}
140+
}
141+
}
142+
```
143+
100144
### **...**
101145

102146
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn rotate(matrix: &mut Vec<Vec<i32>>) {
3+
let n = matrix.len();
4+
for i in 0..n / 2 {
5+
for j in i..n - i - 1 {
6+
let t = matrix[i][j];
7+
matrix[i][j] = matrix[n - j - 1][i];
8+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
9+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
10+
matrix[j][n - i - 1] = t;
11+
}
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)