[LeetCode-Algorithms-12] "Integer to Roman" (2017.10.2-WEEK5)
来源:互联网 发布:淘宝彩票是真的吗 编辑:程序博客网 时间:2024/05/21 09:15
题目链接: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)思路:和上次那个Roman to Integer 一样,需要先搞清楚罗马字母的表示方法。然后用贪心算法,每次选择数字中能表达的最大值。
(2)代码:
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; }};
(3)提交结果:
阅读全文
0 0
- [LeetCode-Algorithms-12] "Integer to Roman" (2017.10.2-WEEK5)
- LeetCode Algorithms #13 <Roman to Integer>
- [LeetCode-Algorithms-13] "Roman to Integer" (2017.9.22-WEEK3)
- [LeetCode 12] Integer to Roman
- leetcode 12 Integer to Roman
- [leetcode 12] Integer to Roman
- [Leetcode] 12 - Integer to Roman
- LeetCode | #12 Integer to Roman
- leetcode.12----------Integer to Roman
- leetcode 12 Integer to Roman
- LeetCode 12 - Integer to Roman
- leetcode 12 -- Integer to Roman
- Leetcode #12-Integer to Roman
- LeetCode 12:"Integer to Roman"
- Leetcode#12 Integer to Roman
- leetcode-12Integer to Roman
- leetcode 12: Integer to Roman
- Leetcode 12 Integer to Roman
- BZOJ4385: [POI2015]Wilcze doły
- 对于Recyclerview的item一些的简单操作
- eclipse maven项目有红叉 但里面没错
- A Wyckoff Bar-by-Bar Analysis Example
- 1
- [LeetCode-Algorithms-12] "Integer to Roman" (2017.10.2-WEEK5)
- GridLayout----计算器
- 指针的大小到底是由谁决定?是多少?
- NOI 1.13 15:求序列中的众数
- Android Donut Makefile 分析之二 (build/envsetup.sh)
- 数据库索引到底是什么,是怎样工作的?
- Windows安装Oracle 11g后开机变慢的解决方法
- 第一课、 走进C
- python dict json读写文件