Kotlin Solution


  • 0
    K

    '''
    val decimalDigits = arrayOf(1, 5, 10, 50, 100, 500, 1000)
    val romansDigits = arrayOf('I', 'V', 'X', 'L', 'C', 'D', 'M')

    fun romanToInt(roman: String): Int {
    if (roman.isEmpty()) return -1
    var pastDigit = 0
    var numberForOutput = 0
    for (i in roman.length - 1 downTo 0) {
    val romanIndex = romansDigits.indexOf(roman[i])
    if (romanIndex < 0) return -1
    val digitNew = decimalDigits[romanIndex]
    if (digitNew >= pastDigit) numberForOutput += digitNew
    else numberForOutput -= digitNew
    pastDigit = digitNew
    }
    return numberForOutput
    }
    ''''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.