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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容