首页 > 代码库 > number-of-segments-in-a-string

number-of-segments-in-a-string

https://leetcode.com/problems/number-of-segments-in-a-string/

 

package com.company;


class Solution {
    public int countSegments(String s) {
        String[] strs = s.split(" ");
        int count = 0;
        for (String str : strs) {
            if (str.length() > 0) {
                count++;
            }
        }
        return count;
    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        String s = "Hello, my  name is John";

        Solution solution = new Solution();
        int ret  = solution.countSegments(s);

        // Your Codec object will be instantiated and called as such:
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}

 

number-of-segments-in-a-string