Skip to content

58. Length of Last Word

难度: Easy

刷题内容

原题连接

  • https://leetcode.com/problems/length-of-last-word

内容描述

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"

Output: 5

解题方案

思路 1 **- 时间复杂度: O(N)*- 空间复杂度: O(N)***

  • 将数组以空格分割,找到最后一个字符串输出长度
  • 注意以空格结尾以及输入空字符串

代码:

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
    var temp = s.split(' ').filter(function (value) {
        return value!='';
    });
    return temp.length>0?temp.pop().length:0;
};


回到顶部