码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode:Count and Say【Python版】

时间:2014-10-18 23:45:06      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   sp   div   on   

一次AC

字符串就是:count+char

 1 class Solution:
 2     # @return a string
 3     def countAndSay(self, n):
 4         str = "1"
 5         for i in range(n-1):
 6             tmp = str
 7             str = ""
 8             c = tmp[0]
 9             cnt = 1
10             for j in range(1,len(tmp)):
11                 if tmp[j] == tmp[j-1]:
12                     cnt += 1
13                 else:
14                     str += ("%d"%cnt + tmp[j-1])
15                     cnt = 1
16             str += ("%d"%cnt + tmp[len(tmp)-1])
17         return str

 

leetcode:Count and Say【Python版】

标签:style   blog   color   io   ar   for   sp   div   on   

原文地址:http://www.cnblogs.com/CheeseZH/p/4033890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!