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
 
这道题跟之前那道Reverse Words in a String有些类似,不过比那题要简单一些,因为不用翻转单词,只要统计出单词的数量即可。那么我们的做法是遍历字符串,遇到空格直接跳过,如果不是空格,则计数器加1,然后用个while循环找到下一个空格的位置,这样就遍历完了一个单词,再重复上面的操作直至结束,就能得到正确结果:
 
解法一:
class Solution {
public:
int countSegments(string s) {
int res = , n = s.size();
for (int i = ; i < n; ++i) {
if (s[i] == ' ') continue;
++res;
while (i < n && s[i] != ' ') ++i;
}
return res;
}
};

下面这种方法是统计单词开头的第一个字符,因为每个单词的第一个字符前面一个字符一定是空格,利用这个特性也可以统计单词的个数:

解法二:

class Solution {
public:
int countSegments(string s) {
int res = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] != ' ' && (i == || s[i - ] == ' ')) {
++res;
}
}
return res;
}
};

下面这种方法用到了C++的字符串流操作,利用getline函数取出每两个空格符之间的字符串,由于多个空格符可能连在一起,所以有可能取出空字符串,我们要判断一下,如果取出的是非空字符串我们才累加计数器,参见代码如下:

解法三:

class Solution {
public:
int countSegments(string s) {
int res = ;
istringstream is(s);
string t = "";
while (getline(is, t, ' ')) {
if (t.empty()) continue;
++res;
}
return res;
}
};

类似题目:

Reverse Words in a String

参考资料:

https://discuss.leetcode.com/topic/70775/c-istringstream-try

https://discuss.leetcode.com/topic/70642/clean-java-solution-o-n

https://discuss.leetcode.com/topic/70656/ac-solution-java-with-trim-and-split

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Number of Segments in a String 字符串中的分段数量的更多相关文章

  1. 【easy】Number of Segments in a String 字符串中的分段数量

    以空格为分隔符,判断一个string可以被分成几部分. 注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格 思路: 只要统计出单词的数量即可.那么我们的做法是遍历字符串,遇到空格直接跳过, ...

  2. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  3. 434 Number of Segments in a String 字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  4. Leetcode434.Number of Segments in a String字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" ...

  5. Leetcode: 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 ...

  6. [Swift]LeetCode434. 字符串中的单词数 | 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 ...

  7. 【LeetCode】434. Number of Segments in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...

  8. 【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 ...

  9. C#LeetCode刷题之#434-字符串中的单词数​​​​​​​(Number of Segments in a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3941 访问. 统计字符串中的单词个数,这里的单词指的是连续的不是 ...

随机推荐

  1. Vertica 7.1安装最佳实践(RHEL6.4)

    一.前期准备工作 1.1各节点IP和主机名 1.2上传脚本并设定环境变量 1.3添加信任 1.4前期准备检查并调整 二.Vertica安装 三.集群性能评估 一.前期准备工作: 1.1各节点IP和主机 ...

  2. 在线课程笔记—.NET基础

    关于学习北京理工大学金旭亮老师在线课程的笔记. 介绍: 在线课程网址:http://mooc.study.163.com/university/BIT#/c 老师个人网站:http://jinxuli ...

  3. VS2010 VS2012 VS2013 VS2015启动调试时老是提示正在下载公共符号

    VS2010 VS2012 VS2013 VS2015启动调试时老是提示正在下载公共符号,下载一些.dll文件,点取消后也能继续调试,但特别慢.解决方法:工具-选项,或者调试-选项和设置,将调试下的& ...

  4. 初学DDD-领域驱动设计

    这几天刚开始学习DDD,看了几篇大神的文章,现在只是知道了几个名词,还没有详细的学习.结合自己的工作经历,说说自己的看法,请各位大神多多指点. 最开始用的比较多的是以数据库表建立模型驱动开发.后来发现 ...

  5. mysql awr 1.0.5 GA正式版发布

    1.0.5变更内容 1.修复centos 7下swap值不正确:2.中文乱码:3.begin/end snap下拉显示Mysql启动时间:4.两次快照间不能重启过:5.新增tab页面查看mysql存储 ...

  6. MS SQL按IN()内容排序

    需求:MMSQL查询结果,按查询条件中关键字IN内的列举信息的顺序一一对应排序. 分析:使用CHARINDEX 函数. 解决方法: SELECT * FROM Product WHERE 1=1 AN ...

  7. JS eval()函数的一些见解

    一.eval是基本使用规则 1 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 2 eval(string) 3 string必需.要计算的字符串,其中含有要计算的 ...

  8. 从无到有实现登录功能以及thinkphp怎么配置数据库信息

    好开心,终于解决了.从学习android到现在写登录功能已经不是一次两次了,如今再写想着肯定是信手拈来,没有想到的是尽然折磨了我一天的时间才搞定它.唉...... 先来看几张截图,这次的登录跟以往的不 ...

  9. Unity3D安卓出包报错

    今天又遇到了在安卓出包时,直接报错了两个错误,报错信息分别如下: Installation failed with the following output: pkg: /data/local/tmp ...

  10. mysql比较时间大小unix_timestamp

    使用unix_timestamp方法进行比较,将字符型的时间,转成unix时间戳 select * from t1 where unix_timestamp(time1) > unix_time ...