diff --git a/length_of_last_word.cpp b/length_of_last_word.cpp new file mode 100644 index 00000000..7cb6c03e --- /dev/null +++ b/length_of_last_word.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int lengthOfLastWord(string s) { + int i = s.size() - 1; + while (i >= 0 && s[i] == ' ') --i; + int len = 0; + while (i >= 0 && s[i] != ' ') { + ++len; + --i; + } + return len; + } +};