題目
原題頁面:https://leetcode.com/problems/count-and-say/
題目型別:String
難度評價:Easy
類似題目:(M) Encode and Decode Strings
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
分析
主要是計數的邏輯。每一輪,遇到一個不同的字元時,就結束到該輪的計數。但是需要注意的是最後一輪的計數,因為遇到的是字串src的結尾,所以需要強制結束這一輪的計數,就類似於編譯原理詞法分析最後的那個$符號的作用。
程式碼
1 | class Solution: |
