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.
Please note that the string does not contain any non-printable characters.

Example:

Input: "Hello, my name is John"
Output: 5

Solution:

思路:

Time Complexity: O(N) Space Complexity: O(1)

Solution Code:

class Solution {
    public int countSegments(String s) {
        int res=0;
        for(int i=0; i

你可能感兴趣的:(434. Number of Segments in a String)