iT邦幫忙

2025 iThome 鐵人賽

DAY 6
0
自我挑戰組

leetcode系列 第 6

leetcode 6. Zigzag Conversion

  • 分享至 

  • xImage
  •  

題目:
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
該字串"PAYPALISHIRING"以鋸齒形圖案在給定數量的行上書寫,如下所示:(您可能希望以固定字體顯示此圖案以獲得更好的可讀性)
然後逐行閱讀:"PAHNAPLSIIGYIR"

編寫程式碼,接受一個字串並根據給定的行數進行轉換
https://ithelp.ithome.com.tw/upload/images/20250920/20169340FlhvlOQ5zv.png

解題思路:
行數 = 1 → 直接回傳原字串。

用方向切換 (goingDown) 來模擬 zigzag 移動。

最後把所有行拼接輸出。


上一篇
leetcode 5. Longest Palindromic Substring
下一篇
leetcode 7. Reverse Integer
系列文
leetcode8
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言