leetcode12----------Integer to Roman

Given an integer, convert it to a roman numeral.

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

罗马数字规则:参考wiki:http://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97

1, 罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。

        罗马数字中没有“0”。

2, 重复次数:一个罗马数字最多重复3次。

3, 右加左减:

在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。

在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。

4, 左减的数字有限制,仅限于I、X、C,且放在大数的左边只能用一个。

(*) V 和 X 左边的小数字只能用Ⅰ。

       (*) L 和 C 左边的小数字只能用X。

      (*) D 和 M 左 边的小数字只能用C。

// LeetCode, Integer to Roman
// 时间复杂度 O(num),空间复杂度 O(1)
class Solution{
public:
	string intToRoman(int num) {
		const int radix[] = { 1000, 900, 500, 400, 100, 90,
			50, 40, 10, 9, 5, 4, 1 };
		const string symbol[] = { "M", "CM", "D", "CD", "C", "XC",
			"L", "XL", "X", "IX", "V", "IV", "I" };
		string roman;
		for (size_t i = 0; num > 0; ++i)
		{
			int count = num / radix[i];
			num %= radix[i];
			for (; count > 0; --count) roman += symbol[i];
		}
		return roman;
	}
};
时间: 2024-08-06 00:45:21

leetcode12----------Integer to Roman的相关文章

04-08 leetcode-12 Integer to Roman

 问题描述:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to3999. 问题分析:类似于十进制,从高位进行计算即可 代码: public class Solution { public String intToRoman(int number) { int[] values = { 1000, 900, 500, 400, 100, 90,

LeetCode12~14 Integer to Roman/Roman to Integer/Longest Common Prefix

一:Integer to Roman 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 链接:https://leetcode.com/problems/integer-to-roman/ 分析:此题关键是确定罗马数字如何表示,比如4并不是IIII而是IV,9并不是VIIII而是IX, 通过不断的取出最高位和余下的得到结果

LeetCode:Integer to Roman

1.题目名称 Integer to Roman (阿拉伯数字到罗马数字的转换) 2.题目地址 https://leetcode.com/problems/integer-to-roman 3.题目内容 英文:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 中文:给出一个整数,将它转换成罗马数字.输入在1-3999之间. 4.题目分

LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则.Wiki了一把,又参考了其它的文档,总结如下: 罗马数字规则: 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 罗马数字中没有"0". 2, 重复次数:一个罗马数字最多重复3次. 3, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上

LeetCode 012 Integer to Roman

[题目] Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. [题意] 给定一个整数,将其表示成罗马数字 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 罗马数组数规则: 基本数字Ⅰ.X .C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成

【LeetCode】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num==0) return sb.toString(); while(num

leetCode 12. Integer to Roman | 字符串 | Medium

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. 题目大意: 将一个给定的阿拉伯数字转换成罗马数字. 思路: 这题看到的时候,想的太多. 其实很简单,将千位,百位,十位,个位都表示出来,然后组合即可. 代码如下: class Solution { public:     string int

[LeetCode][Python]Integer to Roman

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/integer-to-roman/Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.===Comments by Daba

Leetcode | Roman to Integer & Integer to Roman

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 搜了一下,才知道这道题可以写这么简洁. 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 if (s.empty()) return 0; 5 int dict[100]; 6 d

58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字,转换为阿拉伯数字.本题只考虑3999以内的数. 罗马数字有如下符号: Ⅰ(1)Ⅴ(5)Ⅹ(10)L(50)C(100)D(500)M(1000) 计数规则: (1).若干相同数字连写表示的数是这些罗马数字的和,如III=3: (2).小数字在大数字前面表示的数是用大数字减去小数字,如IV=4: (3