Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 컴퓨터과학과
- 리트코드
- Binary Search
- javascript
- greedy
- sliding window
- 방통대
- 자바
- 탐욕알고리즘
- 그리디
- LeetCode
- 자바스크립트
- 투포인터
- Git
- 방송통신대학교
- 알고리즘
- boj
- dynamic programming
- algorithm
- it
- 백준
- 방송대
- 완전탐색
- DP
- 코테
- 코딩
- two pointers
- 이진탐색
- java
- 깃
Archives
- Today
- Total
개발이 취미인 주니어 기획자
[해시맵][JavaScript][LeetCode] #13. Roman to Integer 본문
728x90
반응형
#해시맵 #EASY
Roman to Integer - LeetCode
Can you solve this real interview question? Roman to Integer - 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 tw
leetcode.com
🌷 문제 설명
✏️ LeetCode 연습문제: Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
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.
🎃 제한 사항
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].

입출력 예
| s | result |
| "III" | 3 |
| "LVIII" | 58 |
| "MCMXCIV" | 1994 |
🌷 내 코드
1. 패턴 생각해가면서 풀기

var romanToInt = function (s) {
const roman = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
let answer = 0;
for (let i = 0; i < s.length; i++) {
const current = roman[s[i]];
const next = roman[s[i + 1]];
if (current < next) {
answer += next - current;
i++;
} else {
answer += current;
}
}
return answer;
};
2. 무지성 코딩

var romanToInt = function(s) {
const roman = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
let answer = 0;
for (let i=0; i < s.length; i++) {
if (roman[s[i+1]] && s[i] === 'I') {
if (s[i+1] === 'V' || s[i+1] === 'X') {
answer--
i++
}
} else if (roman[s[i+1]] && s[i] === 'X') {
if (s[i+1] === 'L' || s[i+1] === 'C') {
answer -= 10
i++
}
} else if (roman[s[i+1]] && s[i] === 'C') {
if (s[i+1] === 'D' || s[i+1] === 'M') {
answer -= 100
i++
}
}
answer += roman[s[i]]
}
return answer;
};
🌷 코멘트
자꾸 제한 조건만 줄줄 따라서 코딩해버리는 나 .. 그러지말자
블로그 내용에 문제가 있다면 댓글 혹은 아래로 연락주세요!
~대가리 꽃밭인 디지털 노마드가 꿈이예요~
🧚♀️ Gyumin Lee
📧 gyumin.q.lee@gmail.com
qminlee723 - Overview
noob. qminlee723 has 8 repositories available. Follow their code on GitHub.
github.com
728x90
반응형
'문제 풀이 > 기본 구현' 카테고리의 다른 글
| [Sorting][JavaScript][LeetCode] #1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2023.05.02 |
|---|---|
| [해쉬맵][JavaScript][LeetCode] #1. Two Sum (1) | 2023.04.11 |
| [Javascript][프로그래머스] 숫자 짝꿍 자바스크립트 (0) | 2023.03.05 |
| [Javascript][프로그래머스] 카드뭉치 자바스크립트 (0) | 2023.02.17 |