C++ STL solution, 2 lines, 4 ms.


  • 1
    M
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int i = s.find_last_not_of(' ');
            return (i == string::npos) ? 0 : (i - s.find_last_of(' ', i));
        }
    };

Log in to reply
 

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