1.Description
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.
Example:
**Input:** "Hello, my name is John" **Output:** 5
2.Solution
#20170223
class Solution(object):
def countSegments(self, s):
"""
:type s: str
:rtype: int
"""
s,count=s.strip()+' ',0
for i in range(len(s)-1):
if s[i]!=' ' and s[i+1]==' ':
count+=1
return count
3.思路
!’ ‘+’ ‘这样的结构意味着有一个连续的字符出现