审题不仔细,提交错了好几次。
成功提交代码
class Solution(object):
def countSegments(self, s):
return len([x for x in s.split(' ') if x])
最终优化代码
class Solution(object):
def countSegments(self, s):
return len(s.split())
心得体会
s.split()和s.split(' ')的区别
s = ", , , , a, eaefa"
print s.split()
[',', ',', ',', ',', 'a,', 'eaefa']
print s.split(' ')
[',', ',', ',', ',', '', '', '', '', '', '', '', 'a,', 'eaefa']
print [x for x in s.split(' ') if x]
[',', ',', ',', ',', 'a,', 'eaefa']
网友评论