题解 | #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]);
}

#华为笔试#
全部评论

相关推荐

昨天 11:05
门头沟学院 运营
仁者伍敌:实习生要工作经验,工作要实习经验
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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