【String-easy】 434. Number of Segments in a String 返回给定字符串有多少个单词。单词是由空格分割的

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;
    }
}

猜你喜欢

转载自blog.csdn.net/xiaojie_570/article/details/92606859