Q6. ZigZag Conversion
Last updated
Was this helpful?
Last updated
Was this helpful?
直达:
The string"PAYPALISHIRING"
is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
And then read line by line:"PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
convert("PAYPALISHIRING", 3)
should return"PAHNAPLSIIGYIR"
这道题更像是一道数学题,关键是找到返回字符串的字符位置和原字符串的字符位置的对应关系。0-n的整数数组(字符串下标)更容易看到规律,例如numRows = 5
对应Z形是
两个竖的间距是2*(numRows-1),在第i行(i>=1 && i<=numRows-2 ),中间要插入一个间距为2*(numRows-i)的数字。
换成字符串时,注意下标不要超过字符串的长度。