[58] 最后一个单词的长度

This commit is contained in:
Wen 2023-03-15 18:12:25 +08:00
parent d2874aa551
commit 3c2d221aed
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
/*
* @lc app=leetcode.cn id=58 lang=javascript
*
* [58] 最后一个单词的长度
*/
// @lc code=start
/**
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function (s) {
let index = s.length - 1;
let result = 0;
let first = false;
while (index >= 0) {
if (s[index] == " ") {
if (first) {
return result;
}
} else {
first = true;
result++;
}
index--;
}
return result;
};
// @lc code=end