leetcode : Count and Say
runtime 0 ms, beats 100% of cpp submissions
O(4^(n/3)) solution with explanation
tags: simulation
🔗 link
📖 description
給定一個數字,以固定型式生成字串。
ex. Input: n = 4 Output: "1211" Explanation: countAndSay(1) = "1" countAndSay(2) = say "1" = one 1 = "11" countAndSay(3) = say "11" = two 1's = "21" countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
🧠 solution
根據需求模擬字串生成。
紀錄當前數字出現了幾次,當遇到下一個不同的數字,字串 += 次數, += 數字,並重製當前狀態,看新的數字。
⏳ time complexity
solution
總時間複雜度 O(4^(n/3))
📝 code
1 | class Solution { |