Saturday, September 9, 2017

434. Number of Segments in a String

https://leetcode.com/problems/number-of-segments-in-a-string/description/
    int countSegments(string s) {
        int cnt = 0;
        char pre = ' ';
        for(char c: s) {
            if(c != ' ' && pre == ' ') {
                cnt++;
                pre = c;
            }
            if(c == ' ')
                pre = ' ';
        }
        return cnt;
    }

No comments:

Post a Comment