We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ab2a0ff commit 793c84dCopy full SHA for 793c84d
.gitignore
@@ -0,0 +1,2 @@
1
+.DS_Store
2
+*.swp
README.md
@@ -14,3 +14,4 @@ LeetCode JavaScript Solution
14
| Median of Two Sorted Arrays | Hard | 2017-10-13 |
15
| Longest Palindromic Substring | Medium | 2018-01-19 |
16
| Reverse Integer | Easy | 2018-01-19 |
17
+| Regular Expression Matching | Hard | 2018-03-09 |
src/lengthOfLastWord.js
@@ -3,6 +3,6 @@
3
* @return {number}
4
*/
5
var lengthOfLastWord = function(s) {
6
- var temp = s.replace(/\ +/g, ' ').split(' ');
7
- return (temp[temp.length - 1] && temp[temp.length -1].length) || (temp[temp.length - 2] && temp[temp.length -2].length) || 0;
+ var temp = s.replace(/\ +/g, ' ').trim().split(' ');
+ return temp[temp.length - 1].length;
8
};
0 commit comments