LeetCode 58. 最后一个单词的长度

Posted by cody1991 on August 9, 2020

简单思路:

58. 最后一个单词的长度

下面是正则表达式的方法

1
2
3
4
5
6
7
8
9
10
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function (s) {
  const list = s.replace(/^\s+|\s+$/g, "").split(" ");
  return list[list.length - 1].length;
};

console.log(lengthOfLastWord("Hello World "));

下面是字符串遍历判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function (s) {
  let count = 0;
  let flag = false;
  for (let i = s.length - 1; i >= 0; i--) {
    const c = s[i];
    if (c !== " ") flag = true;
    if (flag && c === " ") break;
    if (flag) {
      count += 1;
    }
  }
  return count;
};

console.log(lengthOfLastWord("Hello World "));