题解 | #MP3光标位置#

MP3光标位置

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

const readline = require('readline')
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

function test(n,str){
  const arr = new Array(n).fill(0)
  let start = 0,end = n % 4 - 1,cur = 0
  for(const item of str){
    if(item === 'U'){
      if(cur === 0){
        cur = arr.length-1
        if(n < 4){
          start = 0
          end = n-1
        }else{
          start = cur - 3
          end = cur
        }
      }else{
        cur--
        if(Math.abs(cur - end) > 3){
          start = cur
          end = cur + 3
        }
      }
    }else{
      if(cur === arr.length-1){
        cur = 0
        start = 0
        end = 3
      }else{
        cur++
        if(Math.abs(cur - start) > 3){
          end = cur
          start = end - 3
        }
      }
    }
  }
  let res = []
  for(let i=start+1;i<=end+1;i++){
    res.push(i)
  }
  console.log(res.join(' '))
  console.log(++cur)
}

const arr = []
rl.on('line',function(line){
  arr.push(line)
})
rl.on('close',function(){
  test(+arr[0],arr[1])
})

全部评论

相关推荐

头像
05-13 11:19
C++
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务