C++ solution, O(n) without examine i-1


  • 0
    Z
    class Solution {
    public:
        int countSegments(string s) {
            int len = s.size(), ret = 0;
            for(int i=0; i<len; ++i) {
                while( i < len && ' ' == s[i] )  ++i;
                if(i++ < len)   ++ret;
                else break;
                while( i < len && ' ' != s[i] )  ++i;
            }
            return ret;
        }
    };
    

  • 0
    Z
    This post is deleted!

Log in to reply
 

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