输入一个字符串。包含空格和可见字符。长度<=100000。
输出一个字符串,表示反转后结果。
the sky is blue!
blue! is sky the
输出一个字符串,表示反转后结果。
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); rl.on("line", (line) => { // 双指针 const str = line.replace(/\s+/g, " ").trim(); let i = 0; let j = 0; const words: string[] = []; while (j < str.length) { if (str[j] !== " ") { j++; } else { words.unshift(str.substring(i, j)); while (j < str.length && str[j] === " ") { j++; } i = j; } } words.unshift(str.substring(i, j)); console.log(words.join(" ")); rl.close(); });
因为输入的字符串会有空格键和tab键,所以投机取巧通过了~
function reverseStr(str){ let random = Math.random(); let arr = str.replaceAll(' ', random).replaceAll('\t',random).split(random); return arr.filter(item => item !== '').reverse().join(' '); }
!Number.isNaN( item.charCodeAt(0) ) end.unshift(item);
const readline=require('readline'); const rl=readline.createInterface({ input:process.stdin, output:process.stdout }); rl.on('line',(line)=>{ let arr=line.split(' '),brr=[]; arr.forEach((item,index)=>{ if( !Number.isNaN(item.charCodeAt(0))){//非NaN的添加到数组头中 brr.unshift(item); } }) console.log(brr.join(' ')); })