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

思路:

从头到尾遍历,如果是空格,直接跳过。

非空格字符都遍历完后,segment++。

int countSegments(string s)
{
int seg = ;
int len = s.length();
for (int i = ; i < len;)
{
while (i < len && s[i] == ' ')i++;//去掉空格
if (i>=len) break;
while (i < len && s[i] != ' ')i++;
seg++;
} return seg;
}

[leetcode-434-Number of Segments in a String]的更多相关文章

  1. 434. Number of Segments in a String

    原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...

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

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

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

  4. [LeetCode] 434. Number of Segments in a String_Easy

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

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

  6. [LC] 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. 434 Number of Segments in a String 字符串中的单词数

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

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

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

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

随机推荐

  1. NSA永恒之蓝病毒,如何通过360工具修复?

    简介: NSA武器库的公开被称为是网络世界"核弹危机",其中有十款影响Windows个人用户的黑客工具,包括永恒之蓝.永恒王者.永恒浪漫.永恒协作.翡翠纤维.古怪地鼠.爱斯基摩卷. ...

  2. Java IO详解(六)------序列化与反序列化(对象流)

    File 类的介绍:http://www.cnblogs.com/ysocean/p/6851878.html Java IO 流的分类介绍:http://www.cnblogs.com/ysocea ...

  3. 关于jQuery插件imgAreaSelect基础讲解

    关于ImgAreaSelect,  是一jQuery插件,它支持用户通过鼠标拖曳选择图片的一部分,如图片拖曳.图片编辑等~~来具体看一下 1.先下载imgAreaSelect插件 下载地址: 英文:h ...

  4. 无阻塞加载和defer、async

    无阻塞加载 把js放在head里,浏览器是怎么去执行它的呢,是按顺序加载还是并行加载呢?在旧的浏览器下,都是按照先后顺序来加载的,这就保证了加载的js依赖不会发生问题.但是少部分新的浏览器已经开始允许 ...

  5. 2016 UESTC Training for Dynamic Programming

    强行做做试试看吧. http://acm.hust.edu.cn/vjudge/contest/124721#overview 密码:mytrain C - 柱爷与咸鱼神功 一个简单01背包. #in ...

  6. how to install Web logic Server (WLS)

    a) Download artificts from: http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-main ...

  7. 基于底层的 XML 的解析方式详解

    在上一篇博客中,我们介绍了什么是 XML ,http://www.cnblogs.com/ysocean/p/6901008.html,那么这一篇博客我们介绍如何来解析 XML . 部分文档引用:ht ...

  8. SmartCoder每日站立会议09

    站立会议内容 今天约了在一起编程,详细确定各个页面以及消息的添加.发送等一些小的细节. 1.站立会议照片:      2.任务展板 2.燃尽图

  9. Merge INTO的用法参考

    Merge是一个非常有用的功能,类似于MySQL里的insert into on duplicate key. Oracle在9i引入了merge命令, 通过这个merge你能够在一个SQL语句中对一 ...

  10. Java 8——Optional

    本文主要介绍Java 8的 Optional 的简单使用 Address 1 2 3 4 5 6 7 @Data @AllArgsConstructor @NoArgsConstructor publ ...