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))
ย
๐ย ์๊ฐ
ย
![[Leetcode] 6. Zigzag Conversion](https://reo91004.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb4dac9d0-810c-47c4-800c-0ca10b8d0529%2Fde29f40a-05a3-4aec-b112-7e56072966a0%2FLeetCode_Logo_Black.png?table=block&id=5f8aa018-04a3-4c68-a0fd-6b8ed6f0fd1d&cache=v2)
![[Leetcode] 6. Zigzag Conversion](https://reo91004.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb4dac9d0-810c-47c4-800c-0ca10b8d0529%2F45a3427e-47fb-412a-a090-3bf6d1afdb04%2Fleetcode.png?table=block&id=5f8aa018-04a3-4c68-a0fd-6b8ed6f0fd1d&cache=v2)

๋๊ธ