Please help me understand this question description about Zig Zag Pattern


  • 7
    X

    I‘m so confused about the qeustion description, please help me clarify.
    If no one answers, I have to look at your answers instead of to solve by meself first. And that will be sad.

    Suppose the input string is always "ABCDEFGHIJKLMN".

    Case 1:

    If rowNum = 2, what does the zig zag pattern look like? Should C, D and G be inline with the first row or the section row ?

    ACDFGIJLM

    B[]E[]H[]K[]N

    Case 2:

    if rowNum = 4, what does the zig zag pattern look like? How many characters should be placed in the odd columns (suppose index starts from 0)? If there is only one character, same question as case 1, which row should this one character be inline with?

    A[]G[]M

    BEHKN

    CFIL

    D[]J[]

    Case 3:

    if rowNum = 5, how many columns should be between the adjacent two 5-row columns to make the shape like "Z"? Is it still only one or three (as shown below)?

    A[][][]I[]

    BF[][]JN

    C[]GK[]

    D[][]HL[]

    E[][][]M[]


  • 7
    V

    "The input is not always constant".

    Now go on write the string vertically from top to bottom one character each, then diagonally right (bottom to top), top to bottom, so on...

    It means,

    • If say string length is: 14, rows = 2

    You have to print the characters at below indices row by row

    0,2,4,6,8,10,12

    1,3,5,7,9,11,13

    • If rows = 3, it will be

    0,4,8,12

    1,3,5,7,9,11,13

    2,6,10

    • String length = 21, rows = 5

    0,8,16

    1,7,9,15,17

    2,6,10,14,18

    3,5,11,13,19

    4,12,20


  • 0
    X

    Thanks Vgn very much for the clarification :)
    The zigzag pattern is "|/|/|/|/|/|" instead of "|-|-|-|" :P


  • 0
    V

    Sure 'xuyi_sg', np. Yes you are right, we print characters row wise when they are stored in "|/|/|..." manner.

    The logic goes something like this if you are struck.

    1. For first and last row, just print the character at index i(ith row) by incrementing index by 2 * nRows - 2.

    2. Else, for each ith row and kth index,

    Depending on if k is odd or even you can increment the index by either 2 * (nRows - 1 - i) or 2 * i

    1. Check if any elements are missed and cover them.

    Happy coding !


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.