Istringstream with C++ 4ms and One line Python code


  • 0
    A
    class Solution(object):
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        return len(s.strip().split(' ')[-1])
    

    and C++ code:

    class Solution {
    public:
    int lengthOfLastWord(string s) {
        if(s.length()==0) return 0;
        istringstream in(s);
        int i = 0;
        for(string word;in>>word;)
        {
            i = word.length();
        }
        return i;
    }
    };

Log in to reply
 

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