🌗 434. 字符串中的单词数
2022年10月10日
- algorithm
🌗 434. 字符串中的单词数
难度: 🌗
问题描述

解法
class Solution {
    public int countSegments(String s) {
        // 思路:
        // 一次遍历
        int len = s.length();
        int index = 0;
        int res = 0;
        while(index < len) {
            while(index < len && s.charAt(index) == ' ') {
                index ++;
            }
            if(index == len) {
                return res;
            }
            // [index] != ' '
            res ++;
            while(index < len && s.charAt(index) != ' ') {
                index ++;
            }
        }
        return res;
    }
}
输出
