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. Collection<E>、Iterable<T>和Iterator<E>接口

    Collection接口 public interface Collection<E>extends Iterable<E> Collection接口主要包含以下方法: Ite ...

  2. 精准准确的统一社会信用代码正则(js)

    参照标准: <GB_32100-2015_法人和其他组织统一社会信用代码编码规则.> 按照编码规则: 统一代码为18位,统一代码由十八位的数字或大写英文字母(不适用I.O.Z.S.V)组成 ...

  3. yaf学习之——yaf安装

    yaf的github源码地址 https://github.com/laruence/yaf 第一步: 下载dll扩展: http://pecl.php.net/package/yaf/2.3.5/w ...

  4. 关于html表格单元格宽度的计算规则

    * { margin: 0; padding: 0 } body { background: #fafafa } ul,li { list-style: none } h1 { margin: 20p ...

  5. 使用VideoView开发视频总结

    一.VideoView及其相关组件总结 在Android中,播放视频有2种方式,第一种方式是使用MediaPlayer结合SurfaceView来播放,通过MediaPlayer来控制视频的播放.暂停 ...

  6. SQL语句集锦

    -语 句                                功 能 --数据操作 SELECT      --从数据库表中检索数据行和列 INSERT      --向数据库表添加新数据行 ...

  7. hdu4417

    hdu4417 题意 给定一个数列,每次查询一个区间,和一个值h,问区间内有多少个数小于等于h. 分析 二分数的个数,划分树求解判断是否满足条件,划分树求解的是第k小的数,那么前面k个数肯定不大于这个 ...

  8. WEB前端:浏览器(IE+Chrome+Firefox)常见兼容问题处理--01

    兼容问题目录 1.IE6下怪异盒模型 2.IE6下最小高度问题 3.IE6下不支持1px的点线 4.IE6下内容会把父级的高度撑开 5.IE6下只支持给a标签添加伪类 6.IE67下不支持给块标签加d ...

  9. go服务端----使用dotweb框架搭建简易服务

    使用dotweb框架搭建简易服务 go语言web框架挺多的,所谓琳琅满目,里面也有很多优秀的,比如echo.beego等,但体验下来,总是觉得哪里有点小疙瘩,后来才明白过来,echo太简单,很多日常使 ...

  10. boost.property_tree读取中文乱码问题正确的解决方式

    开发项目的时候在使用boost,在宽字符下遇到中文乱码问题 上网上看大家都是先转成utf8在进行解析的,例如: http://blog.csdn.net/hu_jiangan/article/deta ...