Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 6 · Parsing / Conversion

Zigzag Conversion

LeetCode 6 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Row index bounces 0 → numRows−1 → 0.

நடுத்தரம்String
LeetCode இல் திற ↗

நான் Zigzag Conversion (LeetCode 6) ஐ இப்படித் தீர்த்தேன் — Parsing / Conversion பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Characters bounce down the rows and back up. Simulate that bounce: append each character to its current row, flip direction at the top and bottom, and concatenate the rows at the end.

Approach

  1. 1

    First version: lay characters out in a 2-D grid and read row by row.

  2. 2

    Final version: one string per row.

  3. 3

    Append s[i] to the current row; flip direction at either edge; move the row.

  4. 4

    Join the rows.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Grid simulationO(n) time · O(n) space

Literally draw the zigzag.

2
Row bucketsAcceptedO(n) time · O(n) space

Bounce between rows.

Grid simulationPythonO(n) time · O(n) space
class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows==1:
            return s
        chars_per_block = 2*numRows-2
        blocks = ceil(len(s)/chars_per_block)
        cols_per_block = numRows-1
        cols = cols_per_block*blocks
        grid = [[""]*cols for _ in range(numRows)]
        i = 0
        cur_row = 0
        cur_col=0
        while i<len(s):
            while i<len(s) and cur_row<numRows:
                grid[cur_row][cur_col]=s[i]
                cur_row+=1
                i+=1
            cur_row-=2
            cur_col+=1
            while i<len(s) and cur_row>0:
                grid[cur_row][cur_col]=s[i]
                cur_row-=1
                cur_col+=1
                i+=1
        res=""
        for i in range(numRows):
            for j in range(cols):
                if grid[i][j]!="":
                    res+=grid[i][j]
        return res
Row bucketsPythonO(n) time · O(n) space
class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows==1:
            return s
        i=0
        down=True
        cur_row=0
        arr=[""]*numRows
        while i<len(s):
            arr[cur_row]+=s[i]

            if cur_row==0:
                down=True
            elif cur_row==numRows-1:
                down=False

            if down==True:
                cur_row+=1
            else:
                cur_row-=1
            i+=1
        return "".join(arr)
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Zigzag Conversion — அடிக்கடி கேட்கப்படும் கேள்விகள்

Zigzag Conversion பின்னாலுள்ள intuition என்ன?

Characters bounce down the rows and back up. Simulate that bounce: append each character to its current row, flip direction at the top and bottom, and concatenate the rows at the end.

Zigzag Conversion (LeetCode 6) ஐ எப்படித் தீர்ப்பது?

1. First version: lay characters out in a 2-D grid and read row by row. 2. Final version: one string per row. 3. Append s[i] to the current row; flip direction at either edge; move the row. 4. Join the rows.

Zigzag Conversion இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(n). (Accepted approach: Row buckets.)

Zigzag Conversion இல் பொதுவான தவறுகள் என்ன?

numRows = 1 must return s unchanged (no bouncing).

Zigzag Conversion ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?

Grid simulation (O(n) time · O(n) space): Literally draw the zigzag. Row buckets (O(n) time · O(n) space): Bounce between rows.