Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

58 返回最后一个单词的长度 #13

Closed
sailei1 opened this issue May 16, 2019 · 0 comments
Closed

58 返回最后一个单词的长度 #13

sailei1 opened this issue May 16, 2019 · 0 comments

Comments

@sailei1
Copy link
Owner

sailei1 commented May 16, 2019

给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。

如果不存在最后一个单词,请返回 0 。

说明:一个单词是指由字母组成,但不包含任何空格的字符串。

示例:

输入: "Hello World"
输出: 5

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
    let arr=s.trim().split(' '); // 去掉两边空格  按空格分割
  
    return arr[arr.length-1].length;
};

@sailei1 sailei1 closed this as completed May 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant