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

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

    and C++ code:

    class Solution {
    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.