leetcode 12. 整数转罗马数字

var intToRoman = function(num) {
  let res = ''
  const numArr = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
  const strArr = ['M','CM','D', 'CD','C','XC','L','XL','X','IX','V','IV','I']
  let len = numArr.length
  for(let i= 0; i< len; i++) {
    while(num >= numArr[i]) {
      num = num - numArr[i]
      res = res + strArr[i]
      if(num === 0) return res
    }
  }
}

 

posted @ 2020-04-14 21:12  想学JS的前端  阅读(144)  评论(0编辑  收藏  举报