diff options
Diffstat (limited to '0006')
| -rw-r--r-- | 0006/Cargo.lock | 7 | ||||
| -rw-r--r-- | 0006/Cargo.toml | 6 | ||||
| -rw-r--r-- | 0006/src/main.rs | 40 |
3 files changed, 53 insertions, 0 deletions
diff --git a/0006/Cargo.lock b/0006/Cargo.lock new file mode 100644 index 0000000..7f8c116 --- /dev/null +++ b/0006/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "p6" +version = "0.1.0" diff --git a/0006/Cargo.toml b/0006/Cargo.toml new file mode 100644 index 0000000..967e5aa --- /dev/null +++ b/0006/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "p6" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/0006/src/main.rs b/0006/src/main.rs new file mode 100644 index 0000000..92e4b6f --- /dev/null +++ b/0006/src/main.rs @@ -0,0 +1,40 @@ +impl Solution { + pub fn convert(s: String, num_rows: i32) -> String { + if num_rows <= 1 { + return s; + } + let mut x = 0; + let mut y = 0; + let mut path: Vec<(i32,i32)> = vec!(); + let mut is_down = true; + for _ in 0..s.len() { + path.push((x, y)); + if is_down { + y = y + 1; + if y == num_rows - 1 { + is_down = false; + } + } else { + y = y - 1; + x = x + 1; + if y == 0 { + is_down = true; + } + } + } + let mut lines: Vec<String> = vec!(); + for _ in 0..num_rows { + lines.push("".to_string()); + } + for i in 0..s.len() { + let (_,y) = path[i]; + lines[y as usize].push(s.as_bytes()[i] as char); + } + lines.join("") + } +} + +struct Solution {} +fn main() { + println!("{}", Solution::convert("PAYPALISHIRING".to_string(), 3)); +}
\ No newline at end of file |
