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<Solution>
想法如下
- 如果當下的字元不是空白,然後前一個字元是空白,代表就遇到一個 segment
C++
Java
Java可以使用 split 這個函數,但要注意的是,如果 input 是 "", split 過後也還是回傳 ""
沒有留言:
張貼留言