标签:style blog color io ar for strong sp 数据
Encode a string by counting the consecutive letter.
(i.e., "aaaabbxxxyyz" might become "a4b2x3y2z1").
分析:
这个问题是一个基本的数据压缩算法,将相邻的字符计数存储。解法没有什么特别需要注意的地方,按部就班的实现代码就可以了。
class Solution: # @param s, letters as string # @return an encoded string def encode(self, s): if not s or len(s) == 0: return "" val = "" prev = s[0] count = 1 for c in s[1:]: if c == prev: count += 1 else: val += prev + str(count) prev = c count = 1 val += prev + str(count) return val if __name__ == ‘__main__‘: s = Solution() assert s.encode("") == "" assert s.encode("aaaabbxxxyyz") == "a4b2x3y2z1" assert s.encode("aaaaaaaaaaab") == "a11b1" print ‘PASS‘
小结:
extends下是目前不在LeetCode但是我遇到的有趣的程序问题。
标签:style blog color io ar for strong sp 数据
原文地址:http://www.cnblogs.com/openqt/p/4040857.html