Skip to content

Instantly share code, notes, and snippets.

@22shubh22
Created October 2, 2019 16:50
Show Gist options
  • Save 22shubh22/06e7a3a793c93f2531b0d8f46884a5cc to your computer and use it in GitHub Desktop.
Save 22shubh22/06e7a3a793c93f2531b0d8f46884a5cc to your computer and use it in GitHub Desktop.
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