每天一道LeetCode--434. Number of Segments in a String
Posted 华裳绕指柔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每天一道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 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的主要内容,如果未能解决你的问题,请参考以下文章
leetcode 434. 字符串中的单词数(Number of Segments in a String)