[Leetcode] 6. Zigzag Conversion
[Leetcode] 6. Zigzag Conversion

[Leetcode] 6. Zigzag Conversion

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 06์›” 14์ผ
floatFirstTOC: right

๐Ÿ–ฅ๏ธย ์‹œ์ž‘ํ•˜๋ฉฐ

๋ฌธ์ž๋ฅผ ์ง€๊ทธ์žฌ๊ทธ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
ย 
  1. ๊ฐ๊ฐ์˜ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•  ๋ฌธ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    1. res = ["" for _ in range(numRows)]
  1. ์ฒ˜์Œ ๋ฐฉํ–ฅ์„ ์ •ํ•œ ํ›„, numRows ๋งŒํผ ์™”๋‹ค๊ฐ€ ๋˜๋Œ์•„๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐฐ์—ด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    1. 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))
ย 

๐Ÿ“Œย ์†Œ๊ฐ

ย 

๋Œ“๊ธ€

guest