【leetcode】434. Number of Segments in a String
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;
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/