题解 | #句子逆序#

句子逆序

http://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3

while(line = readline()) {
    var str = line.trim()
    var index = str.length
    
    var tempArr = []
    var normalScanCount = 0
    while(index--) {
        var ch = str[index]
        if (ch === ' ' && normalScanCount) {
            var word = str.substr(index + 1, normalScanCount)
            tempArr.push(word)
            normalScanCount = 0
        } else {
            normalScanCount++
            if (index === 0) {
                tempArr.push(str.substr(0, normalScanCount))
            }
        }
    }
    console.log(tempArr.join(' '))
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务