LeetCode – 38. Count and Say

題目連結: https://leetcode.com/problems/count-and-say/

參考解答: https://www.itread01.com/content/1546736419.html

func countAndSay(_ n: Int) -> String {
    var result = "1"
    var stringArr: [Character] = []
    for _ in 1 ..< n {
        var index = 0
        stringArr = Array(result)
        result = ""
        while index < stringArr.count {
            var count = 1
            let c = stringArr[index]
            while (index + 1) < stringArr.count && stringArr[index] == stringArr[index+1] {
                index += 1
                count += 1
            }
            result = result + "\(count)\(c)"
            index += 1
        }
    }
    return result
}