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.