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

For example,

Input: "Hello, my name is John"

Output: 5

    public int countSegments(String s) {
String trimmed = s.trim();
if (trimmed.length() == 0)
return 0;
else
return trimmed.split("\\s+").length;
}

每天一道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. java学习路线(好资源大家分享)

    对于入门java将近两年的时间,曾经迷惘过,一直想知道java的具体学习路线,看过了许许多多的java经验分享的帖子,评论,以及其他各种培训机构所谓的学习路线,发现没有一个符合我个人需求的学习路线,根 ...

  2. ueditor 编辑器再thinkphp中使用 解决转义问题

    在前台common.php文件中加入下面的函数就可以解决了 <?php //取消thinkphp里面的转义 if (get_magic_quotes_gpc()) { function stri ...

  3. Android 图像压缩,和LRU算法使用的推荐链接

    近两日,看的关于这些方面的一些教程数十篇,最好的当属google原版的教程了.国内有不少文章是翻译这个链接的. 需要注意的一点是:Android的SDK中的LRU算法在V4包和Util包中各有一个,推 ...

  4. c语言全局变量和局部变量问题汇总

    .局部变量是否能和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,须要使用"::" 局部变量能够与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会用到全局变 ...

  5. MVC4数据注释与验证 2

    Using Validation Annotations Required必须项验证属性 [Required] public string FirstName { get; set; } [Requi ...

  6. 嵌入式设备上运行AllJoyn注意事项

    1. 交叉编译AllJoyn库.编译成功后的文件位于:alljoyn-3.3.0-src\build\linux\arm\debug\dist\目录下: 2. 程序要使用AllJoyn,必须要启动al ...

  7. iOS retain、strong、weak、assign

    iOS retain.strong.weak.assign strong与weak是由ARC新引入的对象变量属性xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和a ...

  8. [Java] MAP、LIST、SET集合解析

    在JAVA的util包中有两个所有集合的父接口Collection和Map,它们的父子关系: java.util        +Collection 这个接口extends自 --java.lang ...

  9. Windows XP与Windows 7系统常见漏洞

    1.Windows XP系统常见漏洞 Windows XP系统常见的漏洞有UPNP服务漏洞.升级程序漏洞.帮助和支持中心漏洞.压缩文件夹漏洞.服务拒绝漏洞.Windows Media Player漏洞 ...

  10. Excel两行交换及两列交换,快速互换相邻表格数据的方法

    经常使用办公软件的人可能有遇到过需要将Excel相邻两行数据相互交换的情况,需要怎么弄才最方便呢?您还是像大家通常所做的那样先在Excel文件相应位置插入一个新的空白行然后在复制粘贴数据然后删除原来那 ...