Integer to Roman--LeetCode
来源:互联网 发布:新网域名转出到阿里云 编辑:程序博客网 时间:2024/06/06 20:39
1.题目
Integer to Roman
2.代码
class Solution {public: string intToRoman(int num) { string res = ""; char roman[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'}; int value[] = {1000, 500, 100, 50, 10, 5, 1}; for (int n = 0; n < 7; n += 2) { int x = num / value[n]; if (x < 4) { for (int i = 1; i <= x; ++i) res += roman[n]; } else if (x == 4) { res = res + roman[n] + roman[n - 1]; } else if (x > 4 && x < 9) { res += roman[n - 1]; for (int i = 6; i <= x; ++i) res += roman[n]; } else if (x == 9) { res = res + roman[n] + roman[n - 2]; } num %= value[n]; } return res; }};
阅读全文
0 0
- 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
- 【leetcode】Roman to Integer
- Android 使用updatefun 来自动更新
- Python库总结——配置相关库(ConfigParser,logging,sys,getopt)
- linux 练习五 如何生成库.so及被C和C++程序可用
- java分布式架构Dubbo+Zookeeper+ActiveMQ+Redis+Nginx+Netty+Oracle+RocketMQ+Linux,教学视频+项目实战
- 单点登录
- Integer to Roman--LeetCode
- hdu oj 1116 Play on Words(并查集+欧拉路径)
- Ubuntu下如何卸载Mysql数据库
- liunx 利用ssh传输文件
- 【Scikit-Learn 中文文档】密度估计
- 11.26语句 程序流程变量 if & switch 的总结
- Canvas常用的方法
- hdu 4323 Magic Number 编辑距离
- LeetCode121. Best Time to Buy and Sell Stock