problem
434. Number of Segments in a String
solution1:
当前字符不为空且前一个字符为空,或者字符串首字符不为空,则为一个分割串。利用的是每一个分割串的前一个字符为空的特性,注意第一个分割串。
class Solution { public: int countSegments(string s) { int res = 0; for(int i=0; i<s.size(); i++) { if(s[i]!=' '&&(i==0 || s[i-1]==' ')) res++; } return res; } };
参考
1. Leetcode_434. Number of Segments in a String;
完