From f036d5e2612a9fe934898322a86a654c56e2898f Mon Sep 17 00:00:00 2001 From: Yashasvi Misra Date: Sun, 24 Oct 2021 09:06:03 +0000 Subject: [PATCH] Add ZigZag Conversion --- .../ZigZag Conversion/ZigZag Conversion.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 LeetCode/Problems/Python/ZigZag Conversion/ZigZag Conversion.py diff --git a/LeetCode/Problems/Python/ZigZag Conversion/ZigZag Conversion.py b/LeetCode/Problems/Python/ZigZag Conversion/ZigZag Conversion.py new file mode 100644 index 00000000..61cbf906 --- /dev/null +++ b/LeetCode/Problems/Python/ZigZag Conversion/ZigZag Conversion.py @@ -0,0 +1,16 @@ +class Solution: + def convert(self, s: str, numRows: int) -> str: + if numRows < 2: + return s + i = 0 + res = [""]*numRows # We will fill in each line in the zigzag + for letter in s: + if i == numRows-1: # If this is the last line in the zigzag we go up + grow = False + elif i == 0: #Otherwise we go down + grow = True + res[i] += letter #Add the letter to its row + i = (i+1) if grow else i-1 # We increment (add 1) if grow is True, + # and decrement otherwise + + return "".join(res) # return the joined rows \ No newline at end of file