题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let k = 0; let len = 0; let commands = []; rl.on('line', function(line){ if(k === 0){ len = Number(line); k++; }else{ commands = line.split(''); getSelection(len, commands); } }); function getSelection(len, commands){ const list = []; for(let i=1; i<=len; i++){ list.push(i); } const start = 0; const end = len<4? len: 4; let display = list.slice(start, end); let cursor = 0; for(const command of commands){ if(command === 'U'){ cursor--; }else if(command === 'D'){ cursor++; } if(cursor === -1){ display = list.slice(-4, len); cursor = cursor + len; } else if(cursor === len){ display = list.slice(0,4); cursor = cursor - len; }else{ const item = list[cursor]; if(command === 'U' && display.includes(item) === false){ display = list.slice(cursor, cursor + 4); }else if(command === 'D' && display.includes(item) === false){ display = list.slice(cursor-3, cursor+1); } } } console.log(...display); console.log(list[cursor]); }
#华为笔试#