floatFirstTOC: right
๐ฅ๏ธย ์์ํ๋ฉฐ
๋ฌธ์๋ฅผ ์ง๊ทธ์ฌ๊ทธ๋ก ์ถ๋ ฅํ๋ ๋๋ ๋ฌธ์ ์
๋๋ค.
ย
- ๊ฐ๊ฐ์ ๋ฌธ์์ด์ ์ ์ฅํ ๋ฌธ์๋ฅผ ์์ฑํฉ๋๋ค.
res = ["" for _ in range(numRows)]
- ์ฒ์ ๋ฐฉํฅ์ ์ ํ ํ,
numRows
๋งํผ ์๋ค๊ฐ ๋๋์๊ฐ๋ ๋ฐฉ์์ผ๋ก ๋ฐฐ์ด์ ์ ์ฅํฉ๋๋ค.
res = ["" for _ in range(numRows)] index = 0 direction = 1 for c in s: res[index] += c if index == 0: direction = 1 elif index == numRows - 1: direction = -1 index += direction
ย
โ๏ธย Python
from typing import List class Solution: def convert(self, s: str, numRows: int) -> str: if numRows == 1: return s res = ["" for _ in range(numRows)] index = 0 direction = 1 for c in s: res[index] += c if index == 0: direction = 1 elif index == numRows - 1: direction = -1 index += direction return "".join(res) if __name__ == "__main__": sol = Solution() print(sol.convert("PAYPALISHIRING", 3)) print(sol.convert("PAYPALISHIRING", 4)) print(sol.convert("A", 1))
ย
๐ย ์๊ฐ
ย
๋๊ธ