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(转换整数到罗马字符)的更多相关文章

  1. leetcode:Integer to Roman(整数转化为罗马数字)

    Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...

  2. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  3. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...

  4. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  5. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  6. 【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 ...

  7. LeetCode题解——Integer to Roman

    题目: 将整数转换为罗马数字.罗马数字规则可以参考: 维基百科-罗马数字 解法: 类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换. 代码: class Solution { publ ...

  8. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  9. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

随机推荐

  1. 安装RabbitMQ3.6.10报错:{error,{missing_dependencies,[crypto,ssl],

    参考https://blog.csdn.net/u010739551/article/details/80848993 如果安装上篇博文安装则可避免这种情况 CentOS6.7安装RabbitMQ3. ...

  2. ansible应用

    前言: 假如让你在一组服务器安装某个软件,服务器少的话还可以接受,但如果有上百台服务器的话,这样会耗费大量时间,在这时候Ansible就由此而生:总之Ansible提供的很多模块十分强大. 一.关于a ...

  3. Nothing is impossible

    题记: <你凭什么上北大>--贺舒婷.依稀记得这篇文章是我高二的时候在<青年文摘>读到的,从此她就成了我为之奋斗的动力.北大,也是我梦中的学府,虽然自己也曾刻苦过,但是还是没有 ...

  4. 微信小程序组件swiper

    视图容器swiper:官方文档 Demo Code Page({ data:{ imgUrls: [ 'http://img02.tooopen.com/images/20150928/tooopen ...

  5. Python单元测试框架——unittest

    测试的常用规则 一个测试单元必须关注一个很小的功能函数,证明它是正确的: 每个测试单元必须是完全独立的,必须能单独运行.这样意味着每一个测试方法必须重新加载数据,执行完毕后做一些清理工作.通常通过se ...

  6. python项目 配置文件 的设置

    一项目目录: 二:默认配置settings的配置:config 文件 __inint__.py文件: #!/usr/bin/env python # -*- coding: utf-8 -*- # C ...

  7. 20145229吴姗珊逆向BOF实践

    20145229吴姗珊逆向BOF实践 实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ...

  8. centos 安装 谷歌BBR

    使用root用户登录,运行以下命令: wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh  ...

  9. 解决Spark用Maven编译时报Exception in thread "main" java.lang.OutOfMemoryError: PermGen space异常

    异常截图: 解决方法: export MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m"

  10. windows 服务器恢复选项恢复

    用户的服务器成了酱紫