본문 바로가기
LeetCode

[LeetCode] 13. Roman to Integer

by JungSeung 2023. 10. 19.
728x90

https://leetcode.com/

Roman numerals are represented by seven different symbols: IVXLCD and 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 one's 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.

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].

 

Code :

/*
	1. value 객체는 로마 숫자와 그에 상응하는 정수 값을 매핑합니다.
	2. 문자열 s를 반복문으로 순회합니다.
	3. 반복문 내에서 현재 문자의 값과 다음 문자의 값을 가져옵니다.
	4. 현재 값이 다음 값보다 작은 경우, 결과에 (다음 값 - 현재 값)을 더하고, 현재 인덱스를 1 증가시킵니다. 이는 로마 숫자에서 흔히 볼 수 있는 예외적인 경우를 처리합니다. 예를 들어, 'IV'는 4를 의미하며 'V' - 'I'로 계산됩니다.
	5. 그렇지 않은 경우에는 현재 값을 결과에 더합니다.
	6. 반복문이 끝나면 최종 결과 값을 반환합니다.
*/
var romanToInt = function(s) {
    let result = 0;
    const value = {
        "I" : 1,
        "V" : 5,
        "X" : 10,
        "L" : 50,
        "C" : 100,
        "D" : 500,
        "M" : 1000 
    }
    for(let i=0; i<s.length; i++){
        
        let current = value[s[i]];
        let next = value[s[i+1]];
        
        if(current < next) {
            result += next - current;
            i++;
        } else {
            result += current;
        }
    }
    return result;
};

 

 

 

출처 : https://leetcode.com/problemset/all/

 

Problems - LeetCode

Boost your coding interview skills and confidence by practicing real interview questions with LeetCode. Our platform offers a range of essential problems for practice, as well as the latest questions being asked by top-tier companies.

leetcode.com

 

728x90

'LeetCode' 카테고리의 다른 글

[LeetCode] 15. 3Sum  (0) 2023.10.20
[LeetCode] 14. Longest Common Prefix  (0) 2023.10.20
[LeetCode] 12. Integer to Roman  (0) 2023.10.19
[LeetCode] 11. Container With Most Water  (0) 2023.10.18
[LeetCode] 10. Regular Expression Matching  (0) 2023.10.18