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.