c++ concise solution, o(n)


  • 0
    S

    int countSegments(string s) {
    int res = 0, i = 0;
    while (i < s.size()) {
    while (i < s.size() && s[i] == ' ') {
    ++i;
    }
    if (i == s.size()) {
    break;
    }
    while (i < s.size() && s[i] != ' ') {
    ++i;
    }
    ++res;
    }
    return res;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.