[LeetCode][JavaScript]Length of Last Word
2015-08-05 23:51
871 查看
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.
For example,
Given s =
"Hello World",
return
5.
https://leetcode.com/problems/length-of-last-word/
求最后一个单词的长度。
js split方法可以传正则表达式作为参数,处理这题就很方便。
/** * @param {string} s * @return {number} */ var lengthOfLastWord = function(s) { var splitArr = s.trim().split(/ +/); return splitArr ? splitArr[splitArr.length - 1].length : 0; };
相关文章推荐
- 【前端学习笔记】Javascript中的正则表达式
- seajs使用-1 解决冲突和依赖
- ExtJs使用总结
- [学习笔记]JavaScript基础--选项卡
- HYSBZ 1821 [JSOI2010]Group 部落划分(kruskal)(中等)
- [JSOI2008][BZOJ1017] 魔兽地图DotR|树型dp
- 解决js跨域问题的思路及实践
- [学习笔记]JavaScript基础--提取事件
- JS中判断undefined和null类型
- JS中判断undefined和null类型
- JS中判断undefined和null类型
- json 作为配置文件
- js中ajax的异步性
- JavaScript-基本语法和数据类型
- jsoup抓取网页+详细讲解
- javascript高级编程技术 第三章
- JSP基础
- Js学习第三天----字符集
- javascript实现html页面之间的参数传递
- 万能JS运动框架