【Leetcode】【python】Number of Segments in a String
2017-07-11 01:04
525 查看
题目大意
计算字符串中的非空子串的个数。解题思路
split()代码
return len(s.split())
总结
这题对于python来说有点智障,然而智障的我还是把他想复杂了,我写的是:class Solution(object): def countSegments(self, s): """ :type s: str :rtype: int """ ss = list(s) count = 1 flag = 0 for i in range(len(ss)): if ord(ss[i])>=48 and ord(ss[i])<=57: flag = 0 continue elif ord(ss[i])>=65 and ord(ss[i])<=90: flag = 0 continue elif ord(ss[i])>=97 and ord(ss[i])<=122: flag = 0 continue else: if flag == 1: flag = 0 continue # print(ss[i], ord(ss[i])) count +=1 flag = 1 return count
提交后是错的,因为:
Input: "love live! mu'sic forever" Output: 5 Expected: 4
之后才意识到自己想复杂了,有空格就可以了。。。
不过代码中flag的作用是记录上一轮循环发生的结果,在其他代码编写中中可以作为一个用例。
相关文章推荐
- LeetCode434 Number of Segments in a String java and python solution
- leetcode oj java Number of Segments in a String
- 20170223-LeetCode_434_Number of Segments in a String
- 【LeetCode】Number of Segments in a String 解题报告
- [LeetCode] Number of Segments in a String 字符串中的分段数量
- Leetcode Number of Segments in a String
- Leetcode 434 Number of Segments in a String
- leetcode[Number of Segments in a String]//待整理多种解法
- Leetcode 434 Number of Segments in a String
- Leetcode: Number of Segments in a String
- LeetCode : Number of Segments in a String
- Leetcode Number of Segments in a String 434
- LEETCODE--Number of Segments in a String
- LeetCode Number of Segments in a String
- leetcode 434[easy]---Number of Segments in a String
- 20170223-leetcode-091-Number of Segments in a String
- leetcode 434- Number of Segments in a String
- Number of Segments in a String
- Number of Segments in a String
- 434_Number_of_Segments_in_a_String