Day18. Number of Segments in a
2017-12-05 本文已影响0人
前端伊始
问题描述:
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.
Example
Input: "Hello, my name is John"
Output: 5
/**
* @param {string} s
* @return {number}
*/
var countSegments = function(s) {
var count = 0;
var arr = s.split(' ');
for(var i in arr){
if(arr[i] !== ""){
count++;
}
}
return count;
};
文末彩蛋
欧盟,美国,中国,俄罗斯,日本
卫星