my easy understand js solution


  • 0
    L

    sorry for my poor English~ and I do not know how to use markdown~
    1、去掉开头和结尾的空格 delete the head and tail spaces
    2、把中间的连续两个以上的空格变成一个 change more than two spaces into one space

    var countSegments = function(s) {
    s = s.replace(/(^\s*)|(\s*$)/g,"").replace(/\s{2,}/g," ");
    if(s=="")
    return 0;
    str = s.split(" ");
    return str.length;
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.