Skip to content

[3] Roman to Integer #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[3] Roman to Integer #69

wants to merge 2 commits into from

Conversation

itpark1018
Copy link
Collaborator

ex) [3] Roman to Integer

ex) 문제 설명

Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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 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.

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

코드 설명

class Solution {
    public int romanToInt(String s) {

        HashMap<Character, Integer> romanValues = new HashMap<>();
        romanValues.put('I', 1);
        romanValues.put('V', 5);
        romanValues.put('X', 10);
        romanValues.put('L', 50);
        romanValues.put('C', 100);
        romanValues.put('D', 500);
        romanValues.put('M', 1000);
        
        int result = 0;
        int preValue = 0;

        for (int i = s.length() - 1; i >= 0 ; i--) {

            int value = romanValues.get(s.charAt(i));

            if (value < preValue) {
                result -= value;
            } else {
                result += value;
            }

            preValue = value;
        }

        return result;
    }
}

해쉬맵을 생성해 각 로마 숫자에 대해서 대응하는 값을 저장한 뒤 문자열의 오른쪽에서 왼쪽순으로 각 로마 숫자에 해당하는 값을 더해줬다.
로마 숫자 특성 때문에 preValue 값을 정해서 현재 값보다 이전 값이 더 크다면 뺄셈이 되게 했다.

@itpark1018 itpark1018 added the coding test 코딩테스트 라벨 label May 8, 2024
@itpark1018 itpark1018 self-assigned this May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
coding test 코딩테스트 라벨
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant