0434.cpp (430B)
1 class Solution { 2 public: 3 int countSegments(const string &s) const { 4 const int n = size(s); 5 int res = 0; 6 7 for (int i = 0; i < n;) { 8 if (s[i] != ' ') { 9 while (i < n && s[i] != ' ') 10 i++; 11 res++; 12 } else { 13 while (i < n && s[i] == ' ') 14 i++; 15 } 16 } 17 18 return res; 19 } 20 };