File tree 1 file changed +40
-6
lines changed
1 file changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -13,9 +13,38 @@ struct Solution;
13
13
14
14
impl Solution {
15
15
16
- // TODO: Implement
17
- pub fn convert ( _s : String , _num_rows : i32 ) -> String {
18
- "" . to_string ( )
16
+ pub fn convert ( s : String , num_rows : i32 ) -> String {
17
+ if num_rows <= 1 { s }
18
+ else {
19
+ let n = num_rows as usize ;
20
+ let mut down = true ;
21
+ let mut row = 0 ;
22
+ let mut holding = vec ! [ vec![ ] ; n] ;
23
+
24
+ for c in s. chars ( ) {
25
+ holding[ row] . push ( c) ;
26
+ if down {
27
+ if row == n-1 {
28
+ down = false ;
29
+ row = n-2 ;
30
+ } else {
31
+ row += 1 ;
32
+ }
33
+ } else {
34
+ if row == 0 {
35
+ down = true ;
36
+ row = 1 ;
37
+ } else {
38
+ row -= 1 ;
39
+ }
40
+ }
41
+ }
42
+
43
+ holding
44
+ . iter ( )
45
+ . map ( |row| row. iter ( ) . collect :: < String > ( ) )
46
+ . collect ( )
47
+ }
19
48
}
20
49
21
50
}
@@ -24,7 +53,6 @@ impl Solution {
24
53
mod tests {
25
54
use super :: Solution ;
26
55
27
- #[ ignore]
28
56
#[ test]
29
57
fn example_1 ( ) {
30
58
let s = "PAYPALISHIRING" . to_string ( ) ;
@@ -33,7 +61,6 @@ mod tests {
33
61
assert_eq ! ( result, "PAHNAPLSIIGYIR" ) ;
34
62
}
35
63
36
- #[ ignore]
37
64
#[ test]
38
65
fn example_2 ( ) {
39
66
let s = "PAYPALISHIRING" . to_string ( ) ;
@@ -42,7 +69,6 @@ mod tests {
42
69
assert_eq ! ( result, "PINALSIGYAHRPI" ) ;
43
70
}
44
71
45
- #[ ignore]
46
72
#[ test]
47
73
fn example_3 ( ) {
48
74
let s = "A" . to_string ( ) ;
@@ -51,4 +77,12 @@ mod tests {
51
77
assert_eq ! ( result, "A" ) ;
52
78
}
53
79
80
+ #[ test]
81
+ fn two_rows ( ) {
82
+ let s = "ABCDEFGHIJKLMNOP" . to_string ( ) ;
83
+ let num_rows = 2 ;
84
+ let result = Solution :: convert ( s, num_rows) ;
85
+ assert_eq ! ( result, "ACEGIKMOBDFHJLNP" ) ;
86
+ }
87
+
54
88
}
You can’t perform that action at this time.
0 commit comments