반응형
https://leetcode.com/problems/roman-to-integer/
Roman to Integer - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
fun romanToInt(s: String): Int {
var answer = 0
for (i: Int in s.indices) {
answer += if (i == s.length - 1) {
RomanNumerals.get(s[i])
} else {
val current = RomanNumerals.get(s[i])
val next = RomanNumerals.get(s[i + 1])
if (current >= next) {
+current
} else {
-current
}
}
}
return answer
}
}
object RomanNumerals {
private val table = mapOf(
'I' to 1,
'V' to 5,
'X' to 10,
'L' to 50,
'C' to 100,
'D' to 500,
'M' to 1000
)
fun get(roman: Char): Int {
return requireNotNull(table[roman])
}
}
https://github.com/960813/leetcode-problems/commit/3b639d2d19de7d17305e0e9258214066a543444f
[013] Kotlin · 960813/leetcode-problems@3b639d2
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
github.com
반응형
☕️ Networking
기술 직군의 기술적인 교류, 커리어 이야기, 직군 무관 네트워킹 모두 환영합니다!
위클리 아카데미 오픈 채팅방(비밀번호: 9323)
kakaotalk: https://open.kakao.com/o/gyvuT5Yd