250x250
반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자 수니

[LeetCode/Swift] 13. Roman to Integer 본문

Algorithm

[LeetCode/Swift] 13. Roman to Integer

개발자 수니 2024. 1. 22. 21:02
728x90
반응형

💡 문제 (Easy)

 

LeetCode - The World's Leading Online Programming Learning Platform

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

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

로마 숫자는 일곱 가지 다른 기호로 나타낼 수 있습니다: I, V, X, L, C, D 및 M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

 

예를 들어, 2는 로마 숫자로 II로 표기되며, 단순히 두 개의 1을 합한 것입니다. 12는 XII로 표기되며, 단순히 X + II입니다. 숫자 27은 XXVII로 표기되며, XX + V + II입니다.

로마 숫자는 일반적으로 왼쪽에서 오른쪽으로 큰 순서대로 작성됩니다. 그러나 4를 나타내는 기호는 IIII가 아닙니다. 대신, 4는 IV로 작성됩니다. 왜냐하면 1이 5 앞에 있으므로 1을 빼서 4가 되기 때문입니다. 동일한 원칙이 숫자 9에도 적용되어 IX로 표기됩니다. 뺄셈이 사용되는 여섯 가지 경우가 있습니다:

  • I는 V(5)와 X(10) 앞에 놓여 4와 9를 만들 수 있습니다.
  • X는 L(50)과 C(100) 앞에 놓여 40과 90을 만들 수 있습니다.
  • C는 D(500)와 M(1000) 앞에 놓여 400과 900을 만들 수 있습니다.

주어진 로마 숫자를 정수로 변환하세요.

 

 

Example 1:

Input: s = "III"
Output: 3
Explanation: III = 3.

 

Example 2:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

 

Example 3:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

Constraints:

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

👩🏻‍💻 해결

roman(Charater : Int 딕셔너리) : 로마 숫자를 딕셔너리로 선언

out(Int) : 최종 결과 숫자를 담을 변수

prev(Int) : 로마 숫자는 일반적으로 왼쪽에서 오른쪽으로 큰 순서대로 작성됨. But! 왼쪽 숫자가 더 클 경우 뺄셈으로 계산됨. 이를 고려하여 왼쪽(앞) 숫자와 오른쪽(뒷) 숫자를 비교 후 연산하기 위해 왼쪽(앞) 숫자를 담을 변수

class Solution {
    func romanToInt(_ s: String) -> Int {
        let roman: [Character: Int] = ["I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000]
        
        var out = 0
        var prev = 0
        
        for char in s {
            let current = roman[char] ?? 0
            out += (current <= prev) ? prev : -prev
            prev = current
        }
        
        out += prev

        return out
    }
}

 

728x90
반응형
Comments