leetcode刷题记录--Reverse Words in a String III

题目

难度: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 String III)