add: 最后一个单词的长度

This commit is contained in:
2020-05-10 18:05:18 +08:00
parent 8a69d1cfb0
commit 6036e99c52
3 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,10 @@
/**
* @param {string} s
* @return {number}
*/
export const lengthOfLastWord = function (s) {
s = s.trim()
const tmp = s.lastIndexOf(' ')
const lastWord = s.substring(tmp === -1 ? 0 : tmp + 1, s.length)
return lastWord.length
}