LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
给定一个整数,将他转换到罗马字符。同样这里的罗马字符不会超过1000(M),PS:关于罗马字符的介绍看我的这篇文章 :LeetCode OJ:Roman to Integer(转换罗马字符到整数)
这里的大体思路是这样的,例如对于罗马字符952来说,起答题的可以分成三个组成部分, 就是 (1000 - 100):CM, (50): L, (1 + 1): II 这三个部分, 再看看1996, 实际上由1000 : M, (1000 - 100): CM, (100 - 10): XC, 5 : V, 1: I
也就是说900 400 90 40 9 4分别需要用两个的字母来表示,这些表示方法都是有限的,所以可以用一个vector意义列举出来,具体程序如下,比较难想到,一开始我也没想到, 看了 这篇文章 才懂了。代码如下:
class Solution {
public:
string intToRoman(int num) {
vector<int> digitNumber{, , , , , , , , , , , , };
vector<string> alpha{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string result = "";
int sz = digitNumber.size();
for(int i = ; num != 0; ++i){
while(num >= digitNumber[i]){
num -= digitNumber[i];
result.append(alpha[i]);
}
}
return result;
}
};
LeetCode OJ:Integer to Roman(转换整数到罗马字符)的更多相关文章
- leetcode:Integer to Roman(整数转化为罗马数字)
Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- Leetcode(8)字符串转换整数
Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...
- [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题解——Integer to Roman
题目: 将整数转换为罗马数字.罗马数字规则可以参考: 维基百科-罗马数字 解法: 类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换. 代码: class Solution { publ ...
- 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 ...
随机推荐
- Django项目在nginx上面的部署(django+flup+nginx)
首先说下需要用到的软件1.python环境因为我用的centos6.2 上面自带了python2.6.6的环境.所以就可以不用安装了.2.Django环境我们这里用easy_install的方式来安装 ...
- HDU1069:Monkey and Banana(最长上升子序列的应用)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 这题挺简单的,给定一个箱子的长宽高,要求啰箱子,但必须保证下面箱子的长和宽必须大于上面的箱子. 一个 ...
- Flash本地共享对象 SharedObject
以下内容是对网上一些资料的总结 Flex SharedObject 介绍(转自http://www.eb163.com/club/thread-3235-1-1.html): Flash的本地共享对象 ...
- HTML 块级元素与行内元素
1.块元素一般都从新行开始,它可以容纳内联元素和其他块元素,常见块元素是段落标签'P".“form"这个块元素比较特殊,它只能用来容纳其他块元素. 2.如果没有css的作用,块元素 ...
- Keras实践:实现非线性回归
Keras实践:实现非线性回归 代码 import os os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" import ke ...
- jsp、freemarker、velocity对比
在java领域.表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术长处:1.功能强大,能够写java代码2.支持jsp标签(jsp tag)3.支持表达式 ...
- 并查集模板 && 带权并查集模板
不带权: ]; void init(void) { ;i<=n;i++) f[i]=i; } int fd(int x) { return f[x]==x?x:fd[x]=fd(f[x]); } ...
- phpword使用
composer 安装 https://packagist.org/packages/phpoffice/phpword 开发文档:http://phpword.readthedocs.io/en/l ...
- qt打包问题。启动失败:Application failed to start because platform plugin “windows” is missing
qt打包启动失败:Application failed to start because platform plugin “windows” is missing 通常的原因是因为没有platform ...
- 2062326 齐力锋 实验二《Java面向对象程序设计》实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程: 程序设计与数据结构 班级: 1623 姓名: 齐力锋 学 ...