58. Length of Last Word

Leetcode

題目

Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

解答

  • 方法一

var lengthOfLastWord = function(s) {
    const arr = s.trim().split(' ');
    return arr[arr.length - 1].length;
};

Runtime: 76 ms, faster than 60.73% of JavaScript online submissions for Length of Last Word.

Memory Usage: 39.2 MB, less than 12.68% of JavaScript online submissions for Length of Last Word.

Last updated

Was this helpful?