美文网首页
leetcode刷题记录--Reverse Words in a

leetcode刷题记录--Reverse Words in a

作者: fishliu | 来源:发表于2018-01-12 11:01 被阅读0次

    题目

    难度:easy

    Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

    Example 1:

    Input: "Let's take LeetCode contest"
    Output: "s'teL ekat edoCteeL tsetnoc"
    

    Note: In the string, each word is separated by single space and there will not be any extra space in the string.

    第一次解法

    /**
     * @param {string} s
     * @return {string}
     */
    var reverseWords = function(s) {
        let res =''
        s.split(" ").map((item)=>{
            let l = item.length
            while(l>=0){
                res += item.charAt(l)
                l--
            }
            res += ' '
        })
        return res.substring(0,res.length-1)
    };
    # runtime : 77 ms
    

    相关文章

      网友评论

          本文标题:leetcode刷题记录--Reverse Words in a

          本文链接:https://www.haomeiwen.com/subject/fnbxoxtx.html