以空格为分隔符,判断一个string可以被分成几部分。

注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格

思路:

只要统计出单词的数量即可。那么我们的做法是遍历字符串,遇到空格直接跳过,如果不是空格,则计数器加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;
}
};

【easy】Number of Segments in a String 字符串中的分段数量的更多相关文章

  1. [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 ...

  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. [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 ...

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

  7. LeetCode_434. Number of Segments in a String

    434. Number of Segments in a String Easy Count the number of segments in a string, where a segment i ...

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

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

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

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

随机推荐

  1. Windows下安装配置MongoDB

    Windows下安装配置MongoDB 一,介绍 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB ...

  2. Swoole入门到实战打造高性能赛事直播平台(完整版)

    需要 的联系我,QQ:1844912514

  3. windows 平台使用 VS2017 编译openssl源码

    windows 平台使用 VS2017 编译openssl源码 1)依赖安装 安装 perl 脚本解释器 下载 http://libevent.net/download 安装 nasm 汇编器 C:\ ...

  4. IntelliJ IDEA 2018 最新版注册码

    参考:IntelliJ IDEA 2018注册码(无需修改hosts文件) :

  5. [ffmpeg] 多输入滤波同步方式(framesync)

    滤波也不总是单一的输入,也存在对多个输入流进行滤波的需求,最常见的就是对视频添加可视水印,水印的组成通常为原视频以及作为水印的图片或者小动画,在ffmpeg中可以使用overlay滤波器进行水印添加. ...

  6. java获取文件行数

    public long getLineNumber(File file) { if (file.exists()) { try { FileReader fileReader = new FileRe ...

  7. UNION的使用方法 (表与表直接数据和在一起的示例)

    SELECT o.CATEGORY CATEGORY,o.KEY_WORK KEY_WORK FROM BO_EU_KEY_WORK wo RIGHT OUTER JOIN BO_EU_WORK_ON ...

  8. linux服务器上,yum、rpm、源码编译安装及卸载

    源码的编译安装及卸载 源码安装三部曲 1.生成makefile编译文件./configure 一般安装包下面都有一个configure文件,用来生成makefile编译文件常用的参数: --prefi ...

  9. 属性的get、set

    以年龄为例,通过属性,控制年龄的输入范围. 静态调用非静态时,需要通过对象来调用. namespace ConsoleApplication1 { class Program { private in ...

  10. python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)

    python全栈开发中级班全程笔记 第三章:员工信息增删改查作业代码 作业要求: 员工增删改查表用代码实现一个简单的员工信息增删改查表需求: 1.支持模糊查询,(1.find name ,age fo ...