Simple C++ (without using standard APIs)


  • 1
    G
        int countSegments(string s) {
            int cnt = 0;
            bool word_started = false;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] != ' ') {
                    if (!word_started) {
                        word_started = true;
                        cnt++;
                    }
                } else word_started = false;
            }
            return cnt;
        }
    

Log in to reply
 

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