Created
October 2, 2019 16:50
-
-
Save 22shubh22/06e7a3a793c93f2531b0d8f46884a5cc to your computer and use it in GitHub Desktop.
conversion https://leetcode.com/submissions/detail/266243610/ to rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Solution { | |
pub fn first(t : i32 , n: i32) -> i32 { | |
2*(n-t) - 1 | |
} | |
pub fn second(t : i32) -> i32 { | |
2*t - 3 | |
} | |
pub fn convert(s: String, num_rows: i32) -> String { | |
let mut ans = String::new(); | |
let indexCounter = 0; | |
let len = s.len(); | |
if num_rows==1 || len == 1 || len <= num_rows { | |
s | |
} | |
let mut i = 1; | |
while i < num_rows { | |
let mut index = i; | |
let flag = false; | |
while index <= len { | |
ans.push(s.chars().nth(index-1).unwrap()); | |
let gap1 = first (&i, &num_rows); | |
let gap2 = second(i); | |
if i == num_rows { | |
index = index + gap2 + 1; | |
} | |
else if i == 1 { | |
index = index + gap1 + 1; | |
} | |
else { | |
if flag == 0 { | |
index = index + gap1 + 1; | |
flag = true; | |
} | |
else { | |
index = index + gap2 + 1; | |
flag = false; | |
} | |
} | |
} | |
i = i + 1; | |
} | |
ans | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment