Magic347
2015-11-01 12:05:34 +08:00
def find(s):
ret = []
if s is None or len(s) == 0:
return ret
start = 0
curr = s[0]
for i in range(0, len(s)):
if s[i] != curr:
ret.append((start+1, i))
start = i
curr = s[i]
if i == len(s) - 1:
ret.append((start+1, len(s)))
else:
if i == len(s) - 1:
ret.append((start+1, len(s)))
return ret
算法上没有特别之处,只是处理一些边界 case 时要细致一些,提供一些测试用例
find("")
find("10")
(1, 1)
(2, 2)
find("1111")
(1, 4)
find("1")
(1, 1)
find("11111111000001111101111100000001111")
(1, 8)
(9, 13)
(14, 18)
(19, 19)
(20, 24)
(25, 31)
(32, 35)