LeetCode题解——Integer to Roman
题目:
将整数转换为罗马数字。罗马数字规则可以参考: 维基百科-罗马数字
解法:
类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换。
代码:
class Solution {
public:
string intToRoman(int num) {
string result;
const int radix[] = {, , , , , , , , , , , , }; //基数,添加了40、90等计算之后的结果
const string sym[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; //各基数对应的罗马字符 for(int i = ; num > ; ++i)
{
int count = num / radix[i]; //当前基数的个数
num %= radix[i]; for( ; count > ; --count)
result += sym[i];
} return result;
}
};
LeetCode题解——Integer to Roman的更多相关文章
- [LeetCode 题解]: Interger to Roman
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an i ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- LeetCode——12. Integer to Roman
一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
随机推荐
- [topcoder]HappyLetterDiv2
http://community.topcoder.com/stat?c=problem_statement&pm=13245 就是有字符串,里面的字符可以随意两两消除,如果不等的话,那么最后 ...
- springmvc图片上传
//-------------------------------------上传图片--------------------------------------------------- @Requ ...
- spring @bean注解
1.@bean注解用于注册一个bean到 到ioc容器中.类似于@component注解 2.@configure注解,相当于指明这个类是配置文件 3.@bean还可以指定initMethod,des ...
- jQuery编程基础精华02(属性、表单过滤器,元素的each,表单选择器,子元素过滤器(*),追加方法,节点,样式操作)
属性.表单过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $("div[title=test]")选取title属性为 ...
- ubuntu下显卡管理
1 Ubuntu下卸载ATI显卡驱动并还原开源驱动[转] 首先卸载已经安装的ATI显卡驱动:cd /usr/share/ati/sudo ./fglrx-uninstall.sh 接着执行下面的代码: ...
- ConcurrentHashMap使用示例
ConcurrentHashMap使用示例 发表于2年前(2013-07-12 19:05) 阅读(3660) | 评论(0) 25人收藏此文章, 我要收藏 赞5 如何快速提高你的薪资?-实力拍“ ...
- C++:运算符重载函数之"++"、"--"、"[ ]"、"=="的应用
5.2.5 "++"和"--"的重载 对于前缀方式++ob,可以用运算符函数重载为: ob.operator++() //成员函数重载 或 operator++ ...
- Mysql:常用代码
C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...
- ios ableviewcell的动态加载数据,模仿喜马拉雅动态数据加载
iphone(UITableViewCell)动态加载图片http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Intr ...
- 使用 Oracle GoldenGate 在 Microsoft SQL Server 和 Oracle Database 之间复制事务
使用 Oracle GoldenGate 在 Microsoft SQL Server 和 Oracle Database 之间复制事务 作者:Nikolay Manchev 分步构建一个跨这些平台的 ...