题意:求字符串中被空格分开的片段的个数。
思路:简单模拟。
class Solution {
public:
int countSegments(string s) {
int re = 0;
for(int i = 0; i < s.length(); ++ i) {
if(s[i] != ' ') {
re ++;
while(s[i] != ' ' && i < s.length()) {
i ++;
}
}
}
return re;
}
};