[LeetCode] 434.Number of Segments in a String
来源:互联网 发布:网络黑侠新书 编辑:程序博客网 时间:2024/05/02 15:23
[LeetCode] 434.Number of Segments in a String
- 题目描述
- 解题思路
- 实验代码
题目描述
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
解题思路
这道题主要在于如何寻找到所需要的“字段”,该字段必须满足:1.它的最后一个字符之后是空格;或者,2.它的最后一个字符就是该字符串的最后一个字符;且,3.它之中的每个字符都不是空格。想到这几个条件之后就能很轻松解决这道题了。
实验代码
class Solution {public: int countSegments(string s) { int sum = 0, l = s.length(); for (int i = 0; i < l; i++) if (s[i] != ' ' && (i+1 == s.size() || s[i+1] == ' ')) sum++; return sum; }};
阅读全文
0 0
- 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
- [LeetCode]434. Number of Segments in a String
- 【Leetcode】434. Number of Segments in a String
- 《深入理解java虚拟机》学习笔记05--HotSpot中对象存活判读算法和垃圾收集算法的实现
- 微信公众号不在支持Http调用,安卓手机沦陷
- 根据经纬度获取城市名
- POJ 1789.Truck History
- 基于vue的倒计时demo
- [LeetCode] 434.Number of Segments in a String
- Activity与View的联系
- Linux下如何让jar程序在后台执行
- Oracle里面的用户user无法登录 LOCKED(TIMED)
- IDE使用笔记(持续更新...)
- 博客小白写的第一篇博文--SHL
- Linux 入门 2017.10.10
- 几个好玩的H5案例
- Ehcache学习