LeetCode | Integer to Roman
来源:互联网 发布:lol数据分析平台 编辑:程序博客网 时间:2024/05/16 18:21
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路:
基本思路是将整数按照10进制存到一个数组中,针对每个数根据>5,=5,<5做处理。
代码:
class Solution {public: string intToRoman(int num) { // Start typing your C/C++ solution below // DO NOT write int main() function char number[8] = {' ','I','V','X','L','C','D','M'}; int data[5]; int len = 0; string result; while(num != 0) { data[len++] = num%10; num /= 10; }; for(int i = len - 1; i >=0; i--) { int skip = data[i]/5; int cur = data[i]%5; if(data[i] == 5) { if(skip > 0) { result.push_back(number[2*(i+1)]); } } else if(cur <= 3) { if(skip>0) { result.push_back(number[2*(i+1)]); } for(int j = 0; j < cur;j++) { result.push_back(number[2*i+1]); } } else if(cur == 4) { result.push_back(number[2*i+1]); result.push_back(number[2*(i+1) + skip]); } } return result; }};
- 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
- 怎样从.zip压缩文件安装Eclipse插…
- Ubuntu 12.x-13.x安装VMware&…
- MiniGUI体系结构之一——体系结构概览
- 科学速读法:20分钟让你阅读速度提高3倍
- 数据挖掘的R包和函数的集合
- LeetCode | Integer to Roman
- ubuntu13.04 mnt文件夹下没有hgfs文件夹
- 图片和string的转换
- hdu 4736 This Is The Job The Bear Finds(2013年成都ACM网络赛)
- 逻辑推理相关问题集锦
- MiniGUI 体系结构之二——多窗口管理和控件及控件类
- Centos 免密码登录设置
- gtk体验
- linux 系统命令行查看电池剩余电量