Leetcode#12Integer to Roman

Integer to Roman

Total Accepted: 31922 Total Submissions: 91429My Submissions

Question Solution

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

【题目】

Given a roman numeral, convert it to an integer. Or, Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

【罗马数字】

1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

10~90: {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

100~900: {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

1000~3000: {"M", "MM", "MMM"}.

public class Solution {

public String intToRoman(int num) {

String[][] roman = {

{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},

{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},

{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},

{"", "M", "MM", "MMM"}

};

String ret = "";

int digit = 0;

while (num != 0) {

int remain = num % 10;

ret = roman[digit][remain] + ret;

digit++;

num /= 10;

}

return ret;

}

}

时间: 2024-10-18 03:56:05

Leetcode#12Integer to Roman的相关文章

LeetCode之12---Integer to Roman

题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题目大意: 输入一个整形数字,将其转化为罗马数字,范围在1--3999之间. 思路: 在做此题之前特意谷歌了一下罗马数字是什么鬼,因为之前只认识罗马数字的1---10(=.=),将维基百科的解释粘贴如下: 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(1

[leetcode] Integer to Roman @ Python

题目: https://oj.leetcode.com/problems/integer-to-roman/ Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 思路:不是很清楚. 代码: class Solution: # @return a string def intToRoman(self, num): ints = [100

LeetCode解题报告--Roman to Integer

题目:罗马数字转为阿拉伯数字 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 分析:题意:将给定的罗马数字转为阿拉伯数字 从前往后遍历罗马数字,如果某个数比前一个数小,则把该数加入到结果中: 反之,则在结果中两次减去前一个数并加上当前这个数: java 代码:(accepted) import java.util.HashMap;

[LeetCode]Integer to Roman

int型数字转化为罗马数字的形式 思路: 由于只是1到3999,一共只有四位,分别求这四位的情况. 可以将每一位从1到9,int和罗马数字的一一对应的关系给出来,然后直接转换. /***************************************************************** Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr

[LeetCode] NO.13 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [题目解析] 这道题很关键的是要理解罗马数字的几个主要的代表符号和表示方式,罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000). 用来表示数字的时候,如果相邻的两个罗马数字,前面表示的数字比后面的小,那么就要减去前面的

【LeetCode】6 - Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Solution: 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 个位数举例 Ⅰ-1.Ⅱ-2.Ⅲ-3.Ⅳ-4.Ⅴ-5.Ⅵ-6.Ⅶ-7.Ⅷ-8.Ⅸ-9 十位数举例 Ⅹ-10.Ⅺ-11.Ⅻ-12.XIII-13.XIV-14.

【leetcode】13. Roman to Integer

题目描述: Given a roman numeral, convert it to an integer. 解题思路: 罗马计数法有以下规则: 基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: 不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: V 和 X 左边的小数字只能用 Ⅰ: L 和 C 左边的小数字只能用X: D

LeetCode——Integer to Roman

Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. noting to say. public class Solution { public String intToRoman(int number) { int[] values = {1000, 900, 500, 400, 100, 90, 50, 4

LeetCode: Interger to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 相对应的一道题:Roman to Interger : http://www.cnblogs.com/double-win/p/3760002.html 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 str