summaryrefslogtreecommitdiff
path: root/src/bin/p0006.rs
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-08-21 00:52:09 +0800
committerMistivia <i@mistivia.com>2025-08-21 00:52:09 +0800
commit1e0b0feaa83ea60ff06db81276ef0935182a4bd9 (patch)
treebff3d6980b891702122b88806e9475f655f4dbec /src/bin/p0006.rs
parent3d10c45d6bc630e1c3c8c359592c1808d28acf75 (diff)
solve 6
Diffstat (limited to 'src/bin/p0006.rs')
-rw-r--r--src/bin/p0006.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/bin/p0006.rs b/src/bin/p0006.rs
new file mode 100644
index 0000000..92e4b6f
--- /dev/null
+++ b/src/bin/p0006.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