🌗 434. 字符串中的单词数

吞佛童子2022年10月10日
  • algorithm
  • String
小于 1 分钟

🌗 434. 字符串中的单词数

难度: 🌗

问题描述

img_7.png


解法

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

输出

img_6.png

上次编辑于: 2022/10/10 下午8:43:48
贡献者: liuxianzhishou