【Leetcode】【python】Number of Segments in a String
来源:互联网 发布:免费查询大数据征信 编辑:程序博客网 时间:2024/05/22 05:16
题目大意
计算字符串中的非空子串的个数。
解题思路
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:5Expected:4
之后才意识到自己想复杂了,有空格就可以了。。。
不过代码中flag的作用是记录上一轮循环发生的结果,在其他代码编写中中可以作为一个用例。
阅读全文
0 0
- [leetcode: Python]434. Number of Segments in a String
- 【Leetcode】【python】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 434 Number of Segments in a String
- Leetcode Number of Segments in a String 434
- LeetCode 434. Number of Segments in a String
- 【Leetcode 434. Number of Segments in a String】
- leetcode 434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- leetcode oj java Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- Leetcode 434. Number of Segments in a String (Easy) (cpp)
- 【LeetCode】 434. Number of Segments in a String
- [leetcode]434. Number of Segments in a String
- [leetcode]434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- CSS盒模式
- 商品信息表
- cocos2d读取plist文件
- opencv跟踪手掌 代码
- Linux系统--浅谈标准I/O缓冲区
- 【Leetcode】【python】Number of Segments in a String
- 支持向量机
- eclipse Git插件Team中不显示
- Windows核心编程
- MapReduce模型初探(二)
- android 解析未安装apk中的AndroidManifest.xml以及系统源码分析
- 封装+继承+多态-零星知识点
- AVL树的插入算法
- 使用从源码编译的SITL测试dronekit代码