1234567891011121314151617181920212223242526 |
- package main
- // iterator of countAndSay function
- func casIter(last string, n int) string {
- if n == 1 {
- return last
- }
- next := make([]rune, 0)
- curr := last[0]
- cnt := 1
- for i := 1; i < len(last); i++ {
- if curr != last[i] {
- next = append(next, rune(cnt+'0'), rune(curr))
- curr = last[i]
- cnt = 1
- } else {
- cnt++
- }
- }
- next = append(next, rune(cnt+'0'), rune(curr))
- return casIter(string(next), n-1)
- }
- func countAndSay(n int) string {
- return casIter("1", n)
- }
|