Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.1 Public classSolution {2 Public intRomantoint (String s) {3 intRes=0;4hashmapNewHashmap();5Map.put ("M", +); Map.put ("CM", "Map.put"); Map.put ("CD", 400);6Map.put ("C", +); Map.put ("XC", 90);7Map.put ("L", "X"), Map.put ("XL", "9"), Map.put ("X.", "Map.put"), Map.put ("IX"), 5 ("V"););8Map.put ("IV", 4); Map.put ("I", 1);9
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.Solution:1 Public classSolution {2 Public intRomantoint (String s) {3 if(S.length () ==0)return0;4 5mapNewHashmap();6Map.put (' I ', 1);7Map.put (' V ', 5);8Map.put (' X ', 10);9Map.put (' L ', 50);TenMap.put (' C ', 100); OneMap.put (' D ', 500); AMap.put (' M ', 1000); - - intres = 0; the intindex = 0; -
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999Roman Numeral Counting Method:
Basic characters
I
V
X
L
C
D
M
The corresponding Arabic numerals are expressed as
1
5
10
50
100
500
1000
1, the same number ligatures, the number is equal to the number of the sum of these numbers, such as: ⅲ= 3, 2, the small n
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.Subscribe to see which companies asked this questionⅠ (1) Ⅴ (5) Ⅹ (Ten) L (+) C (+) D (+) M (1000)Rule: At the back of a large number as a addend, in the front of the large number as a meiosissuch as: ⅲ=3,ⅳ=4,ⅵ=6,ⅹⅸ=19,ⅹⅹ=20,ⅹlⅴ=45,mcmⅹⅹc=1980Starting from the lowest bit, if the previous number is larger than it, let the previous value precede
Integer to RomanGiven an integer, convert it to a Roman numeral.Input is guaranteed to being within the range from 1 to 3999.Do the processing by interval.Solution One: Non-recursiveclassSolution { Public: stringInttoroman (intnum) { stringret; //m while(Num >= +) {ret+='M'; Num-= +; } //to here, num //cm if(Num >= the) {ret+="CM"; Num-= the; } //to here, num //d if(Num >= -) {ret+='D'; Num-= -; } /
Given an integer, convert it to a Roman numeral.Input is guaranteed to being within the range from 1 to 3999.Analysis: This problem reference leetcode.pdf solution, code concise, processing ingenious. The following points are worth reference:(1) 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 as the base, thus eliminating the need to handle special cases such as 900,400,90,40,9,4 alone.(2) It is easier to calculate from a large base, since there
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.Solution:
Basic characters
I
V
X
L
C
D
M
The corresponding Arabic numerals are expressed as
1
5
10
50
100
500
1000
Single digit examples ⅰ-1, ⅱ-2, ⅲ-3, ⅳ-4, ⅴ-5, ⅵ-6, ⅶ-7, Ⅷ-8, ⅸ-9
10-digit examples ⅹ-10, ⅺ-11, ⅻ-12, XII
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.1 classSolution {2 Public:3 intRomantoint (strings) {4 intLength =s.length ();5 if(Length 1)return 0;6mapChar,int>m;7m['I'] =1;8m['V'] =5;9m['X'] =Ten;Tenm['L'] = -; Onem['C'] = -; Am['D'] = -; -m['M'] = +; - inti = length-1; the intsum = m[s[i--]]; - while(i>=0) - if(m[s[i+1]] >M[s[i]])
Given an integer, convert it to a Roman numeral.Input is guaranteed to being within the range from 1 to 3999.1 stringInttoroman (intnum) {2 stringRes;3 Chara[4]={'I','X','C','M'};4 Charb[3]={'V','L','D'};5 intI=3, ld=0, n= +;6 while(i>=0)7 { 8ld=num/N;9num%=N;TenN/=Ten; One A Switch(LD) - { - Case 0: Break; the Case 1: {Res+=a
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.1InlineintC2N (Charc) {2 Switch(c) {3 Case 'I':return 1; 4 Case 'V':return 5; 5 Case 'X':return Ten; 6 Case 'L':return -; 7 Case 'C':return -; 8 Case 'D':return -; 9 Case 'M':return +; Ten default:return 0; One } A } - classSolution { - Public: the
Given an integer, convert it to a Roman numeral.Input is guaranteed to being within the range from 1 to 3999.classSolution { Public: stringInttoroman (intnum) { intValues[] = { +, the, -, -, -, -, -, +,Ten,9,5,4,1};//initialization of the array stringNumerals[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" }; intSize =sizeof(values)/sizeof(values[0]);//get the size of an array stringresult =""; for(inti =0; i )
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.classSolution { Public: intRomantoint (strings) {intnum =0; intn =s.size (); for(inti =0; I i) { if(I 1 Tonumber (S[i]) 1])) {num+ = Tonumber (S[i +1]) -Tonumber (S[i]); ++i; } Else{num+=Tonumber (S[i]); } } returnnum; } intTonumber (Charch) { Switch(CH) { Case 'I':return 1; Case 'V':return 5;
Problem Description: Given An integer, convert it to a Roman numeral.Input is guaranteed to being within the range from 1 to3999.Problem analysis: Similar to decimal, it can be calculated from highCode:public class Solution {public String Inttoroman (int number) {int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] numerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; StringBuilder result = n
Given a Roman numeral, convert it to an integer.Input is guaranteed to being within the range from 1 to 3999.AC one of the fastest. Each type of letter represents a number. The small on the left side means right minus left, and small on the right side for right plus left. If IV represents 4, VI means 6.I, I:1V, V:5X, X:10L, l:50C, c:100D, d:500M, m:10001 classSolution {2 Public:3 intRomantoint (strings) {4 intTimes =1;5 intresult
This article describes how php converts Arabic and Roman numerals. it involves php string operations related skills and has some reference value, for more information about how to convert Arabic and Roman numerals in php, see the following example. Share it with you for your reference. The details are as follows:
I hope this article will help you with php programming.
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
The following is a supplement to Baidu's Roman numerals:
Single digit exampleI, 1] II, 2] III, 3] IV, 4] V, 5] vi, 6] VII, 7] VIII, 8] IX, 9· Dozens of examplesX, 10] Xi, 11] XII, 12] XIII, 13] XIV, 14] XV, 15] XVI, 16] XVII, 17] XVIII, 18] XIX, 19] XX, 20] XXI, 21] XXII, 22] XXIX, 29] XXX, 30] XXXIV,
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Question: Conversion Method: scan the Rome character from left to right. If the number corresponding to the current character is smaller than the previous one, add the number directly; otherwise, add this number and subtract twice the previous number, and then update the previous number. Use a hashmap to store the correspondence between
Roman numerals
Value
I
1
II
2
III
3
IV
4
V
5
VI
6
VII
7
VIII
8
IX
9
X
10
XI
11
XII
12
XIII
13
XIV
14
XV
15
XVI
16
XVII
17
XVIII
18
XIX
19
XX
20
XXX
30
XL
40
L
50
LX
Integer to RomanGiven an integer, convert it to a Roman numeral.Input is guaranteed to being within the range from 1 to 3999.Using greedy algorithm1 classSolution {2 Public:3 stringInttoroman (intnum) {4 5 stringresult="";6 stringsymbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 7 intvalue[]= { +, the, -, -, -, -, -, +,Ten,9,5,4,1};8 9 intI=0;Ten while(num!=0) One
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Answer
public class Solution { Map
Roman to integer
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.