mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
@ -688,6 +688,58 @@ public class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Ruby#:
|
||||||
|
```ruby
|
||||||
|
def generate_matrix(n)
|
||||||
|
result = Array.new(n) { Array.new(n, 0) }
|
||||||
|
#循环次数
|
||||||
|
loop_times = 0
|
||||||
|
#步长
|
||||||
|
step = n - 1
|
||||||
|
val = 1
|
||||||
|
|
||||||
|
|
||||||
|
while loop_times < n / 2
|
||||||
|
#模拟从左向右
|
||||||
|
for i in 0..step - 1
|
||||||
|
#行数不变,列数变
|
||||||
|
result[loop_times][i+loop_times] = val
|
||||||
|
val += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
#模拟从上到下
|
||||||
|
for i in 0..step - 1
|
||||||
|
#列数不变,行数变
|
||||||
|
result[i+loop_times][n-loop_times-1] = val
|
||||||
|
val += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
#模拟从右到左
|
||||||
|
for i in 0..step - 1
|
||||||
|
#行数不变,列数变
|
||||||
|
result[n-loop_times-1][n-loop_times-i-1] = val
|
||||||
|
val += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
#模拟从下到上
|
||||||
|
for i in 0..step - 1
|
||||||
|
#列数不变,行数变
|
||||||
|
result[n-loop_times-i-1][loop_times] = val
|
||||||
|
val += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
loop_times += 1
|
||||||
|
step -= 2
|
||||||
|
end
|
||||||
|
|
||||||
|
#如果是奇数,则填充最后一个元素
|
||||||
|
result[n/2][n/2] = n**2 if n % 2
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user