Java time 2ms beats 80%


  • 0
    public int countSegments(String s) {
        int count = 0;
        int length = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                length++;
                if (i == s.length() - 1) {
                    if (length > 0) {
                        count++;
                    }
                }
            } else {
                if (length > 0) {
                    count++;
                }
                length = 0;
            }
        }
        return count;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.