您的位置:首页 > 其它

20170223-LeetCode_434_Number of Segments in a String

2017-02-23 09:49 471 查看

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.思路

!’ ‘+’ ‘这样的结构意味着有一个连续的字符出现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: