[LeetCode] ZigZag Conversion
문제
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)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI"
Approach
규칙을 찾아보면 첫째줄과 마지막줄은 (row-1) * 2 마다 index를 올리고
위에서부터 n만큼 떨어진 줄은 ((row-1) * 2) - (2 * i), 2 * i씩 인덱스를 올리는 것을 확인할 수 있다.
이 규칙으로 하면되는데 스트링 배열 범위를 잘못생각해서 이상하게 시간을 많이썼다...
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_ZigZagConversion.cpp
chi3236/algorithm
Contribute to chi3236/algorithm development by creating an account on GitHub.
github.com