5 Integer to Roman -- 57ms 这是LeetCode上网友的答案,感觉跟简洁,同时算法也很容易理解,和这里第一种的思想很像: classSolution{public:stringintToRoman(intnum){ string M[] = {"","M","MM","MMM"}; string C[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; ...
classSolution{public:stringintToRoman(intnum){stringres ="";intdiv[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};stringsymble[13] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};intstart =0;while(num >0){if(num < div[start]) start++...
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 给你一个整数,把它转化为罗马数字,输入保证在1到3999之间。 关于罗马数字介绍: 1、计数方法:① 罗马数字就有下面七个基本符号:Ⅰ(1)、Ⅴ(5)、Ⅹ(10)、L(50)、C(100)、D(500)、M(...
func intToRoman(num int) string { m := map[int]string{ 1:"I", 5:"V", 10:"X", 50:"L", 100:"C", 500:"D", 1000:"M", } var s string base := 1 for ;num != 0; { rem := num % 10 switch{ case rem == 4: s = m[base] + m[5*base] + s case rem == 5:...
Xcan be placed beforeL(50) andC(100) to make 40 and 90. Ccan be placed beforeD(500) andM(1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Example 1: Input: num = 3 Output: "III" Example 2: ...
Input is guaranteed to be within the range from 1 to 3999. class Solution { public: string intToRoman(int num) { 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", ...
data =50;break;case'C': data =100;break;case'D': data =500;break;case'M': data =1000;break; }returndata; } } Integer to Roman 贪心法 复杂度 时间O(N) 空间 O(1) 思路 因为罗马数字都是由最大化的表示,比如10会表示成X而不是VV,所以我们可以从最大可能的表示向小的依次尝试。因为提示...
classSolution{publicStringintToRoman(intnum){int[]values={1000,900,500,400,100,90,50,40,10,9,5,4,1};String[]romanPieces={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};StringBuildersb=newStringBuilder();for(inti=0;i<values.length;i++){while(num>=...
I decided to mess around for a few minutes, and created a Value Converter that takes a value, converts it to an integer, then outputs that integer as a roman numeral. I thought I'd post the results of that here. And, as a kicker, a quick explination of how to use it in ...
Roman To Integer 这是leetcode上的一道题目。 链接 Roman to Integer | LeetCode OJ 题目 将罗马数字转换为整形,输入范围是1-3999 释义 1-3999的罗马数,用到的符号和对应的十进制数如下: I→ 1 V→ 5 X→ 10 L→ 50 C→ 100 D→ 500 M→ 1000...