LeetCode

LeetCode

  • Problems
  • GitHub

›Problems

Problems

  • Two Sum
  • Add Two Numbers
  • Longest Substring Without Repeating Characters
  • Median of Two Sorted Arrays
  • Longest Palindromic Substring
  • ZigZag Conversion
  • Reverse Integer
  • String to Integer (atoi)
  • Palindrome Number
  • Regular Expression Matching
  • Container With Most Water
  • Integer to Roman
  • Roman to Integer
  • Longest Common Prefix
  • 3Sum
  • 3Sum Closest
  • Letter Combinations of a Phone Number
  • 4Sum
  • Remove Nth Node From End of List
  • Valid Parentheses
  • Merge Two Sorted Lists
  • Generate Parentheses
  • Merge k Sorted Lists
  • Swap Nodes in Pairs
  • Reverse Nodes in k-Group
  • Remove Duplicates from Sorted Array
  • Remove Element
  • Implement strStr()
  • Divide Two Integers
  • Substring with Concatenation of All Words
  • Next Permutation
  • Longest Valid Parentheses
  • Search in Rotated Sorted Array
  • Find First and Last Position of Element in Sorted Array
  • Search Insert Position
  • Valid Sudoku
  • Sudoku Solver
  • Count and Say
  • Combination Sum
  • Combination Sum II
  • First Missing Positive
  • Trapping Rain Water
  • Multiply Strings
  • Wildcard Matching
  • Jump Game II
  • Permutations
  • Permutations II
  • Rotate Image
  • Group Anagrams
  • Pow(x, n)
  • Binary Tree Inorder Traversal
  • Triangle
  • Number of Islands
  • Random Pick Index
  • Coin Change 2
  • Maximum Length of Pair Chain
  • Repeated String Match
  • Minimum ASCII Delete Sum for Two Strings
  • Remove Comments
  • Split Linked List in Parts
  • Design HashSet
  • RLE Iterator
  • Number of Recent Calls
  • Rotting Oranges
  • Minimum Number of K Consecutive Bit Flips
  • Remove All Adjacent Duplicates In String
  • Unique Number of Occurrences
  • Count Servers that Communicate
  • Subtract the Product and Sum of Digits of an Integer
  • Find the Smallest Divisor Given a Threshold
  • Find N Unique Integers Sum up to Zero
  • Minimum Flips to Make a OR b Equal to c
  • Combine Two Tables
  • Second Highest Salary
  • Nth Highest Salary
  • Rank Scores
  • Consecutive Numbers
  • Employees Earning More Than Their Managers
  • Duplicate Emails
  • Customers Who Never Order
  • Department Highest Salary
  • Department Top Three Salaries
  • Delete Duplicate Emails
  • Rising Temperature
  • Trips and Users
  • Big Countries
  • Classes More Than 5 Students
  • Valid Phone Numbers
  • Tenth Line
  • Print in Order
  • Maximum Subarray

Roman to Integer

Description

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, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solution(javascript)

/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
    var romanMatrix = [
      [1000, 'M'],
      [900, 'CM'],
      [500, 'D'],
      [400, 'CD'],
      [100, 'C'],
      [90, 'XC'],
      [50, 'L'],
      [40, 'XL'],
      [10, 'X'],
      [9, 'IX'],
      [5, 'V'],
      [4, 'IV'],
      [1, 'I']
    ];
    
    var num = 0;
    for (var i = 0; i < romanMatrix.length; i++ ) {
        while(s.startsWith(romanMatrix[i][1])){
            num += romanMatrix[i][0];
            s = s.slice(romanMatrix[i][1].length, s.length);
        }
    }
    
    return num;
};
← Integer to RomanLongest Common Prefix →
  • Description
  • Solution(javascript)
Powered By LeetCode Site Generator