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".
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 ?
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?
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)?
"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...
- If say string length is: 14, rows = 2
You have to print the characters at below indices row by row
- If rows = 3, it will be
- String length = 21, rows = 5
Thanks Vgn very much for the clarification :)
The zigzag pattern is "|/|/|/|/|/|" instead of "|-|-|-|" :P
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.
For first and last row, just print the character at index i(ith row) by incrementing index by 2 * nRows - 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
- Check if any elements are missed and cover them.
Happy coding !
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.