leetcode【第十七周】单词数量统计

来源:互联网 发布:未来教育计算机二级vb 编辑:程序博客网 时间:2024/06/08 15:34

问题描述:

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

问题分析:

如题所述,要求字符串中的片段数,这里的片段可以简单地理解成单词,因而是单词数的统计。统计方法很简单,从头到尾遍历字符串的每一个字符,读到非空格字符时,就略过接下来的所有非空格字符,并且统计数加一,遍历完整个字符串即可统计所有单词数目。

代码实现:

class Solution {public:int countSegments(string s) {int count = 0;unsigned int i;for (i = 0; i

原创粉丝点击