Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
013.roman_to_integer (Easy)
links:
Title: https://oj.leetcode.com/problems/roman-to-integer/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
Convert the Roman number to decimal.
Analysis:
With 012. Integer to Roman (Medium), just know the conversion rules.
Code:
C++:
Class Solution {Private:int val[255];void init () {val[' I '] = 1; val[' V '] = 5; val[' X '] = ten; val[' L '] = 50;val[' C '] = 100; val[' D '] = 500; val[' M '] = 1000;} Public: int Romantoint (string s) {init (); int ret = 0;for (int i = 0; i < s.size (); i++) {if (i > 0 && VA L[s[i]] > Val[s[i-1]) {ret + val[s[i]]-2 * val[s[i-1]];} else {ret + = Val[s[i]];}} return ret; }};
Java:
public class Solution { private int[] val = new int[255]; private void Init () { val[' I '] = 1; val[' V '] = 5; val[' X '] = ten; val[' L '] = ; val[' C '] = 100; val[' D '] = 500; val[' M '] = +; } public int Romantoint (String s) { init (); int ret = 0; for (int i = 0; i < s.length (); i++) { if (i > 0 && val[s.charat (i)] > Val[s.charat (i-1)]) { R ET + = Val[s.charat (i)]-2 * Val[s.charat (I-1); } else { ret + = Val[s.charat (i)];} } return ret; }}
Python:
Class solution: # @return An integer def romantoint (self, s): val = {' I ': 1, ' V ': 5, ' X ': Ten, ' L ':-10 0, ' D ': +, ' M ': +} ret = 0 for i in range (len (s)): If i > 0 and val[s[i]] > val[s[i-1]: ret + = Val[s[i]]-2 * val[s[i-1] [ else: ret + val[s[i]] return RET
[Leetcode] 013. Roman to Integer (easy) (C++/java/python)