Leetcode 1213:整数转罗马数字-罗马数字转整数
Posted hello,是翠花呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1213:整数转罗马数字-罗马数字转整数相关的知识,希望对你有一定的参考价值。
自己从0到1的思路,仅供参考:
整数转罗马数字
// 整数转罗马数字
function intToRoman(num) {
if (num <= 10) {
return unit(num)
} else if(num <= 100) {
return unit2(num)
} else if (num <= 1000) {
return unit3(num)
} else if (num > 1000) {
return unit4(num)
}
}
function unit(num) {
let I = 'I'
let V = 'V'
let X = 'X'
let result = ''
if (num <= 3) {
for (let i = 0; i < num; i++) {
result += I
}
} else if (num === 4) {
result = I + V
} else if (num === 5) {
result = V
} else if (num > 5 && num < 9) {
result = V
for (let i = 0; i < num - 5; i++) {
result += I
}
} else if (num === 9) {
result = I + X
} else if (num === 10) {
result = X
}
return result
}
function unit2(num) {
let X = 'X'
let L = 'L'
let C = 'C'
let result = ''
if (num < 40) {
for (let i = 0; i < Math.floor(num / 10); i++) {
result += X
}
if (num % 10) {
result += unit(num % 10)
}
} else if (num >= 40 && num < 50) {
result = X + L
result += unit(num - 40)
} else if (num === 50) {
result = L
} else if (num > 50 && num < 90) {
result = L
for (let i = 0; i < Math.floor((num - 50) / 10); i++) {
result += X
}
if (num % 10) {
result += unit(num % 10)
}
} else if (num >= 90 && num < 100) {
result = X + C
result += unit(num - 90)
} else if (num === 100) {
result = C
}
return result
}
function unit3(num) {
let C = 'C'
let D = 'D'
let M = 'M'
let result = ''
if (num < 400) {
for (let i = 0; i < Math.floor(num / 100); i++) {
result += C
}
if (num % 100) {
result += unit2(num % 100)
}
} else if (num >= 400 && num < 500) {
result = C + D
result += unit2(num - 400)
} else if (num === 500) {
result = D
} else if (num > 500 && num < 900) {
result = D
for (let i = 0; i < Math.floor((num - 500) / 100); i++) {
result += C
}
if (num % 100) {
result += unit2(num % 100)
}
} else if (num >= 900 && num < 1000) {
result = C + M
result += unit2(num - 900)
} else if (num === 1000) {
result = M
}
return result
}
function unit4(num) {
let M = 'M'
let result = ''
for (let i = 0; i < Math.floor(num / 1000); i++) {
result += M
}
if (num % 1000) {
result += unit3(num % 1000)
}
return result
}
罗马数字转整数
// 罗马数字转整数
let I = 'I'
let V = 'V'
let X = 'X'
let L = 'L'
let C = 'C'
let D = 'D'
let M = 'M'
function romanToInt(s) {
let result = 0
if (s[0] === I) {
result = unit1(s)
} else if (s[0] === V) {
result = unit2(s)
} else if (s[0] === X) { //
result = unit3(s)
} else if (s[0] === L) {
result = unit4(s)
} else if (s[0] === C) {
result = unit5(s)
} else if (s[0] === D) {
result = unit6(s)
} else if (s[0] === M) {
result = unit7(s)
}
console.log(result)
return result
}
// I
function unit1(s) {
let result = 0
if (s.length <=3 && s[1] !== V && s[1] !== X){
result = s.length
} else if (s.length === 2 && s[1] === V) {
result = 4
} else if (s.length === 2 && s[1] === X) {
result = 9
}
return result
}
// V
function unit2(s){
let result = 0
result = 5 + (s.length -1)
return result
}
// X
function unit3(s) {
let result = 0
let second = s.slice(1)
let third = s.slice(2)
if (s[1] === I) {
return unit1(second) + 10
} else if (s[1] === V) {
return unit2(second) + 10
} else if (s[0] === X && s[1] !== L && s[1] !== C) {
return unit3(second) + 10
}
if (s[1] === L) {
result = 40
if (s[2] === I) {
result += unit1(third)
} else if (s[2] === V) {
result += unit2(third)
}
} else if (s[1] === C) {
result = 90
if (s[2] === I) {
result += unit1(third)
} else if (s[2] === V) {
result += unit2(third)
}
}
return result
}
// L
function unit4(s){
let result = 0
let second = s.slice(1)
if (s[1] === I) {
return unit1(second) + 50
} else if (s[1] === V) {
return unit2(second) + 50
} else if (s[1] === X) {
console.log('l', unit3(second),second)
return unit3(second) + 50
}
result = 50 + (s.length -1) * 10
return result
}
// C
function unit5(s){
let result = 0
let second = s.slice(1)
let third = s.slice(2)
if (s[1] === I) {
return unit1(second) + 100
} else if (s[1] === V) {
return unit2(second) + 100
} else if (s[1] === X) {
return unit3(second) + 100
} else if (s[1] === L) {
return unit4(second) + 100
}
else if (s[0] === C && s[1] !== D && s[1] !== M) {
return unit5(second) + 100
}
if (s[1] === D) {
result = 400
if (s[2] === I) {
result += unit1(third)
} else if (s[2] === V) {
result += unit2(third)
} else if (s[2] === X) {
result += unit3(third)
} else if (s[2] === L) {
result += unit4(third)
}
} else if (s[1] === M) {
result = 900
if (s[2] === I) {
result += unit1(third)
} else if (s[2] === V) {
result += unit2(third)
} else if (s[2] === X) {
result += unit3(third)
} else if (s[2] === L) {
result += unit4(third)
}
}
return result
}
// D
function unit6(s){
let result = 0
let second = s.slice(1)
if (s[1] === I) {
return unit1(second) + 500
} else if (s[1] === V) {
return unit2(second) + 500
} else if (s[1] === X) {
return unit3(second) + 500
} else if (s[1] === L) {
return unit4(second) + 500
} else if (s[1] === C) {
return unit5(second) + 500
}
result = 500 + (s.length -1) * 100
return result
}
// M
function unit7(s){
let result = 0
let second = s.slice(1)
if (s[1] === I) {
return unit1(second) + 1000
} else if (s[1] === V) {
return unit2(second) + 1000
} else if (s[1] === X) {
return unit3(second) + 1000
} else if (s[1] === L) {
return unit4(second) + 1000
} else if (s[1LeetCode-013-罗马数字转整数