[LeetCode] 434. Number of Segments in a String

来源:互联网 发布:it骗婚 编辑:程序博客网 时间:2024/06/16 07:57

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
// Runtime: 0msclass Solution {public:    int countSegments(string s) {        istringstream is(s);        int cnt = 0;        for (string word; is >> word; cnt++)            ;        return cnt;    }};