My many liner in C++


  • 0
    B

    Self explanatory:

    class Solution {
    public:
        int countSegments(string s) {
            int count=0;
            char pre =' ';
            for (char c: s)
              {
              if (c==' ' && pre!=' ') ++count; //transition from word to space
              pre = c;
              }
            return (pre!=' ') ? count + 1 : count;  //if sentence ends with word instead of space
        }
    };

Log in to reply
 

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