[leetcode: Python]434. Number of Segments in a String
来源:互联网 发布:只有我知1 编辑:程序博客网 时间:2024/06/05 20:17
Title:
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
方法一:52ms
class Solution(object): def countSegments(self, s): """ :type s: str :rtype: int """ if s.isspace(): return 0 s1 = s.strip() if len(s1) == 0: return 0 d =[0] m = list(s1) while m: if m[0] != ' ': d[-1] += 1 else: d.append(0) m.pop(0) return len(d) - d.count(0)
方法二:32ms
class Solution(object): def countSegments(self, s): """ :type s: str :rtype: int """ return len(s.split())
…业务不熟练啊
阅读全文
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 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 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
- Leetcode 434. Number of Segments in a String
- Leetcode-434. Number of Segments in a String
- LeetCode-434. Number of Segments in a String
- easyui 单元格编辑,下拉表格。。。。
- 连接查询
- iOS开发 ☞ Runloop使用
- golang如何下载go get不下来(被墙)的第三方包
- android基本功
- [leetcode: Python]434. Number of Segments in a String
- GOLANG实现超时对象检测的最好理解的方式
- Go1.8实现watchdog功能,实现依赖启动服务程序
- Leap Motion(JS)控制(提高事件判定精准度)
- 使用FreeMarker替换JSP的10个理由
- scrapy安装和简单使用
- Android酷炫的25种动画开源框架
- 移动app测试中出现bug漏测的原因分析
- 接口