题解 | #字符逆序#

字符逆序

https://www.nowcoder.com/practice/cc57022cb4194697ac30bcb566aeb47b

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    while(line = await readline()){
        let index = Math.ceil(line.length / 2);
        let line_length = line.length;
        let temp_line = line.split('');
        for(let i = 0; i < index; i++){
            [temp_line[i],temp_line[line_length - i - 1]] = [temp_line[line_length - i - 1],temp_line[i]];
        }
        console.log(temp_line.join(''));
    }
}()

思路:

逐个交换

[a,b] = [b,a] 交换a、b的值

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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