개발이 취미인 주니어 기획자

[해시맵][JavaScript][LeetCode] #13. Roman to Integer 본문

문제 풀이/기본 구현

[해시맵][JavaScript][LeetCode] #13. Roman to Integer

큐 2023. 4. 20. 10:00
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. 패턴 생각해가면서 풀기

1일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
반응형