Question: http://oj.leetcode.com/problems/count-and-say/
Question Name: Count and Say
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Solution: # @return a string def countAndSay(self, n): result = "1" # The result for n == 1 for _ in xrange(n-1): temp = "" # The result for next round current = result[0] currentCount = 1 for item in result[1:]: if item == current: # Same value as previous currentCount += 1 else: # Meet with a new value, store the result of # previous value temp += str(currentCount) + current #Reset these two variables current = item currentCount = 1 # Store the result for the last unique value temp += str(currentCount) + current result = temp return result |