Difficult:Easy
題目
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
翻譯
給定一個由單詞和空格組成的字符串 s,返回字符串中最後一個單詞的長度。
單詞是僅由非空格字符組成的最大子串。
範例
Example 1
1 | Input: s = "Hello World" |
Example 2
1 | Input: s = " fly me to the moon " |
Example 3
1 | Input: s = "luffy is still joyboy" |
解題思路
1.從字串後面開始尋找
2.當找第一個非空白字元時開始記錄長度,再碰到空白字元時結束。
Solution
Code 1 :
1 | var lengthOfLastWord = function (s) { |