File tree Expand file tree Collapse file tree 1 file changed +41
-1
lines changed Expand file tree Collapse file tree 1 file changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -60,4 +60,44 @@ impl Solution {
6060
6161可以发现这里的边界条件非常多,在一个循环中,如此多的边界条件,如果不按照固定规则来遍历,那就是一进循环深似海,从此offer是路人。
6262
63- 然后好像就是我这种模拟的做法
63+ 然后好像就是我这种模拟的做法
64+
65+
66+ ``` rust
67+
68+ # struct Solution {}
69+
70+ impl Solution {
71+ pub fn generate_matrix (n : i32 ) -> Vec <Vec <i32 >> {
72+ let mut m : Vec <Vec <i32 >> = vec! [vec! [0i32 ; n as usize ]; n as usize ];
73+ let mut current : i32 = 1i32 ;
74+ for i in 0usize .. (n as usize + 1usize ) / 2usize {
75+ Self :: f (& mut m , i , & mut current );
76+ }
77+ m
78+ }
79+
80+ pub fn f (m : & mut Vec <Vec <i32 >>, start : usize , start_num : & mut i32 ) {
81+ let width : usize = m . len () - start * 2usize - 1usize ;
82+ if width == 0 { m [start ][start ] = * start_num ; return ; }
83+ for i in 0 .. width {
84+ m [start ][start + i ] = * start_num ;
85+ * start_num += 1i32 ;
86+ }
87+ for j in 0 .. width {
88+ m [start + j ][start + width ] = * start_num ;
89+ * start_num += 1i32 ;
90+ }
91+ for i in 0 .. width {
92+ m [start + width ][start + width - i ] = * start_num ;
93+ * start_num += 1i32 ;
94+ }
95+ for j in 0 .. width {
96+ m [start + width - j ][start ] = * start_num ;
97+ * start_num += 1i32 ;
98+ }
99+ }
100+ }
101+
102+
103+ ```
You can’t perform that action at this time.
0 commit comments