【LeetCode算法练习(C++)】Integer to Roman
来源:互联网 发布:网络打印被挂起 编辑:程序博客网 时间:2024/06/06 04:22
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
ps:
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
链接:Integer to Roman
解法:贪心,每次选择能表示的最大值
class Solution { public: string intToRoman(int num) { string ans; string symbol[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int value[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; for (int i = 0; num != 0; i++) { while (num >= value[i]) { num -= value[i]; ans += symbol[i]; } } return ans; }};
Runtime: 45 ms
阅读全文
0 0
- 【LeetCode算法练习(C++)】Integer to Roman
- 【LeetCode算法练习(C++)】Roman to Integer
- Leetcode算法练习-easy篇-Roman to Integer
- 【LeetCode算法练习(C++)】String to Integer (atoi)
- leetcode系列(35)Roman to Integer & Integer to Roman
- LeetCode OJ算法题(十二):Integer to Roman
- LeetCode OJ算法题(十三):Roman to Integer
- LeetCode算法题之Roman to Integer
- 13. Roman to Integer leetcode算法笔记
- LeetCode算法题目: Integer to Roman
- LeetCode 13. Roman to Integer 题解(C++)
- LeetCode 12. Integer to Roman 题解(C++)
- [C++]LeetCode: 16 Roman to Integer
- [C++]LeetCode: 85 Integer to Roman
- C实现 LeetCode->Integer to Roman
- C实现 LeetCode->Roman to Integer
- 【leetcode c++】13 Roman to Integer
- [leetcode-13]Roman to Integer(C)
- 更改的资料
- 磁芯存储器简介------顺便说说core dump的core是什么含义
- 打印机模拟(彩色篇)
- 【金融财经】金融市场一周简报(2017-09-22)
- PostgreSQL数据库排查脚本规划
- 【LeetCode算法练习(C++)】Integer to Roman
- 目录:已完成
- (转)Android:你要的WebView与 JS 交互方式 都在这里了
- unbuntu下R包安装报错问题
- UVA 10452 Marcus
- HTML中的标签系列-01-简单标签
- c3p0问题java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirector的解决方法
- Java的基础
- HDU 6206 Apple(外心)