Leetcode (7) Integer to Roman
来源:互联网 发布:安顺行知高中地址 编辑:程序博客网 时间:2024/06/04 18:18
题目描述
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
博主表示第一次看的时候,没理解这个题目是啥意思,然后小查了一下才知道原来是要求我们把一个阿拉伯数字转换成罗马数字的形式。根据wiki查了一下罗马数字的规则:
- 罗马数字共有7个,即 I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000);
- I 可以被用在 V 和 X 前面,用来表示4(IV)和9(IX);
- X可以被用在 L 和 C 前面,用来表示 40(XL)和90(XC);
- C 可以被用在 D 和 M 前面,用来表示400(CD)和900(CM)。
罗马数字还有一些其他的规则表示更大的数字,但是这里题目限定了数字最大不超过3999,因此,在了解了上述规则之后,我们就可以开工啦。博主使用的方法相当于查表~~~直接简单暴力保存了每个罗马数字的特殊形式,这里只有13个元素也不多~
class Solution {public: void appendNum(string& s, int& num, int n, string c) { int r = num / n; num -= r * n; while (r--) { s.append(c); } } string intToRoman(int num) { const int n = 13; int numSymbol[n] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 }; string romanSymbol[n] = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" }; string r; for (int i = n - 1; i > -1; --i) { appendNum(r, num, numSymbol[i], romanSymbol[i]); } return r; }};
0 0
- Leetcode (7) Integer to Roman
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- LeetCode Integer to Roman & Roman to Integer
- leetcode - roman to integer & integer to roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- LeetCode: Integer to Roman
- LeetCode : Roman to Integer
- [Leetcode] Roman to Integer
- [Leetcode] Integer to Roman
- Leetcode: Integer to Roman
- Leetcode: Roman to Integer
- LeetCode Integer to Roman
- Navicat 远程连接SQL Server 2014 Express 报08001错误
- NGUI控件说明(中文) UIToggle
- 【动态树】[BZOJ2002] Bounce 弹飞绵羊
- Android的theme主题中统一定义各种控件的style属性
- EditPlus破解
- Leetcode (7) Integer to Roman
- Two pointers技巧的应用
- iOS 打电话 & 发短信 & 发邮件
- Eclipse IDE 使用技巧
- NGUI控件说明(中文) UIPlayAnimation
- C/C++中static,const,inline三种关键字详细总结
- (5-3)Mapper源码分析
- Activity生命周期和其中另外线程的关系
- NGUI控件说明(中文) UIPopupList