All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0013_roman_to_integer.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0001_0100.s0013_roman_to_integer

// #Easy #Top_Interview_Questions #String #Hash_Table #Math
// #2022_03_29_Time_293_ms_(78.06%)_Space_38.2_MB_(81.58%)

class Solution {
    fun romanToInt(s: String): Int {
        var x = 0
        var y: Char
        for (i in s.indices) {
            y = s[i]
            when (y) {
                'I' -> x = getX(s, x, i, 1, 'V', 'X')
                'V' -> x += 5
                'X' -> x = getX(s, x, i, 10, 'L', 'C')
                'L' -> x += 50
                'C' -> x = getX(s, x, i, 100, 'D', 'M')
                'D' -> x += 500
                'M' -> x += 1000
            }
        }
        return x
    }

    private fun getX(s: String, x: Int, i: Int, i2: Int, v: Char, x2: Char): Int {
        var localX = x
        if (i + 1 == s.length) {
            localX += i2
        } else if (s[i + 1] == v) {
            localX -= i2
        } else if (s[i + 1] == x2) {
            localX -= i2
        } else {
            localX += i2
        }
        return localX
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy