Golang solution


  • 0
    P
    package string
    
    import "strconv"
    
    func countAndSay(n int) string {
    	if n == 0 {
    		return ""
    	}
    	res := "1"
    	for ; n > 1; n-- {
    		cur := ""
    		for i := 0; i < len(res); i++ {
    			count := 1
    			for i+1 < len(res) && res[i] == res[i+1] {
    				count++
    				i++
    			}
    			cur += strconv.Itoa(count) + string(res[i])
    		}
    		res = cur
    	}
    	return res
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.