leetcode 434 Number of Segments in a String

Java:

class Solution {
    public int countSegments(String s) {
        int res = 0;
        for(int i=0; i<s.length(); i++){
            if((s.charAt(i)!=' ')&&(i==0||s.charAt(i-1)==' ')) res++;
        }
        return res;
    }
}

Python:

class Solution:
    def countSegments(self, s):
        """
        :type s: str
        :rtype: int
        """
        return len(s.split())

猜你喜欢

转载自blog.csdn.net/mrxjh/article/details/79938374