码迷,mamicode.com
首页 > 其他好文 > 详细

每天一道LeetCode--434. Number of Segments in a String

时间:2016-12-05 23:32:33      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:style   sequence   leetcode   ret   tput   where   turn   etc   public   

 

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

标签:style   sequence   leetcode   ret   tput   where   turn   etc   public   

原文地址:http://www.cnblogs.com/xiaoduc-org/p/6135563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!