1. 题目原址
https://leetcode.com/problems/number-of-segments-in-a-string/
2. 题目描述
3. 题目大意
返回给定字符串有多少个单词。单词是由空格分割的。
4. 解题思路
签到题
5. AC代码
class Solution {
public int countSegments(String s) {
int count = 0;
for (int i = 0; i < s.length(); ++i)
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' '))
++count;
return count;
}
}