add: 最后一个单词的长度
This commit is contained in:
10
src/string/length-of-last-word.js
Normal file
10
src/string/length-of-last-word.js
Normal 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
|
||||
}
|
Reference in New Issue
Block a user