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 ...
随机推荐
- 深入浅出Mybatis-分页
http://blog.csdn.net/hupanfeng/article/details/9265341 http://blog.csdn.net/isea533/article/details/ ...
- Linux内核的同步机制
本文详细的介绍了Linux内核中的同步机制:原子操作.信号量.读写信号量和自旋锁的API,使用要求以及一些典型示例 一.引言 在现代操作系统里,同一时间可能有多个内核执行流在执行,因此内核其实象多进程 ...
- Android 虚线分割Shape
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- 各种分区类型对应的partition_Id
ID Name Note == ==== ==== 00h empty [空] 01h DOS 12-bit FAT [MS DOS FAT12] 02h XENIX root file system ...
- 删除单链表的倒数第k个结点
策略 直接遍历总数为len,再次遍历第len-k+1个就是答案,但是这样遍历了O(N+k)个,可以在O在更短的时间内找到 图示 参考代码 #include <iostream> using ...
- 如何写科技文章的讨论discussion部分
众所周知,讨论部分是在结合自己的研究结果基础上,对整个文章的结论的提炼和升华.这一部分是整个论文的精,往往点睛作用. 同时,很多杂志要求结果和讨论分开,这也就更突出了写好讨论的重要性. 那么,我们应该 ...
- NDK(12)Jni常用函数
参考官方文档 http://docs.oracle.com/javase/7/docs/technotes/guides/jni/ http://docs.oracle.com/javase/7/do ...
- 无法生成临时类(result=1)。 error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性
对于错误无法生成临时类(result=1).error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性 出现这个错 ...
- Provider Pattern提供者模式和策略模式
http://www.codeproject.com/Articles/18222/Provider-Pattern Introduction Provider pattern is one of t ...
- MySQL中REGEXP正则表达式使用大全
REGEXP在mysql是用来执行正则表达式的一个函数 像php中的preg之类的函数了,regexp正则函数如果只是简单的查询使用like即可,但复杂的还是需要使用regexp了,下面我们来看看. ...