티스토리 뷰
Problem
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].
Solution
로마 숫자를 정수로 변환하는 문제입니다.
public class Solution {
private static final String[] ROMANS = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I"};
private static final int[] VALUES = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4,
1};
public int romanToInt(String roman) {
int result = 0;
int offset = 0;
for (int i = 0; i < ROMANS.length; i++) {
while (roman.startsWith(ROMANS[i], offset)) {
result += VALUES[i];
offset += ROMANS[i].length();
}
}
return result;
}
}
쉬운 난이도의 문제라 풀 수 있는 방법도 엄청 다양한데 배열을 이용해 값을 매핑해놓고, 문자열의 offset을 계산하여 현재 해당하는 문자열의 값을 더해나가는 방식으로 구현하였습니다.
Test
package io.lcalmsky.leetcode.roman_to_integer;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class SolutionTest {
@Test
void testAll() {
assertAll(
() -> test("III", 3),
() -> test("LVIII", 58),
() -> test("MCMXCIV", 1994)
);
}
private void test(String given, int expected) {
// when
Solution solution = new Solution();
int actual = solution.romanToInt(given);
// then
assertEquals(expected, actual);
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
804. Unique Morse Code Words (0) | 2022.08.18 |
---|---|
387. First Unique Character in a String (0) | 2022.08.17 |
235. Lowest Common Ancestor of a Binary Search Tree (0) | 2022.08.12 |
98. Validate Binary Search Tree (0) | 2022.08.11 |
377. Combination Sum IV (0) | 2022.08.06 |
- Total
- Today
- Yesterday
- proto3
- Linux
- QueryDSL
- spring boot application
- Spring Boot JPA
- gRPC
- 알고리즘
- 스프링부트
- 스프링 부트 애플리케이션
- 클린 아키텍처
- Spring Boot Tutorial
- @ManyToOne
- 헥사고날 아키텍처
- Java
- 스프링 데이터 jpa
- spring boot app
- Spring Data JPA
- leetcode
- Jackson
- JSON
- 스프링 부트 회원 가입
- 스프링 부트
- intellij
- 스프링 부트 튜토리얼
- r
- spring boot jwt
- Spring Boot
- 함께 자라기 후기
- 함께 자라기
- JPA
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |