计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
看了一下很多都有问题的啊…牛客这里测试用例太少了
if (!s.length) return 0; let num = 0; for (let i = s.length - 1; i >= 0; i--) { if (s.charAt(i) === ' ') { if (num === 0) { continue; } else { break; } } else { num++; } } return num;
function getLastWordLength(words){ return words.substring(words.lastIndexOf(' ')+1).length } const words = readline() console.log(getLastWordLength(words))