0ms C++ solution, easy to understand


  • 1
    M
    int lengthOfLastWord(string s) {
        auto end_idx = s.find_last_not_of(' ');
        if (end_idx == string::npos) return 0;
        auto beg_idx = s.find_last_of(' ', end_idx);
        if (beg_idx == string::npos) return end_idx+1;
        return end_idx - beg_idx;
    }

Log in to reply
 

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