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. springboot接口 接收参数为实体对象跟MultipartFile对象报错。

    要把文件和普通数据类型分开接口传输,不可以兼容多个类型参数, 建议是传文件一个接口,返回url路径,再和普通数据一起提交,就是两次 企业上的做法都是这样,先用文件服务器保存文件,返回文件路径 http ...

  2. 002-主流区块链技术特点及Hyperledger Fabric V0.6版本特点

    一.主流区块链技术特点 二.HyperLedger子项目 三.Hyperledger fabric架构 V0.6逻辑架构: V0.6区块链网络 对应的0.6版本的运行时架构: 0.6版本的架构特点是: ...

  3. Angular学习笔记—路由(转载)

    创建路由 1.首先安装 Angular Router.你可以通过运行以下任一操作来执行此操作: yarn add @angular/router # OR npm i --save @angular/ ...

  4. HDU1165: Eddy's research II(递推)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1165 果断不擅长找规律啊,做这种题静不下心来. Ackermann function can be def ...

  5. sql server 中having 的使用注意事项

    1.having 中不能使用未参与分列的组,having 不能替代where 作用不一样,having是对组进行过滤,where是每条记录进行过滤. 2.having 是对Group By 的条件分组 ...

  6. nginx灰度环境

    1.nginx.conf split_clients "${remote_addr}AAA" $request_type { 25% "abtest"; * & ...

  7. cocos2d-x与着色器设计--入门篇(游云凌天原创)

    http://blog.csdn.net/danjinxiangsi/article/details/43949955 着色器(Shader)应用与计算机图形学领域,指一组提供计算机图形资源在渲染时执 ...

  8. Microsoft.VisualStudio.Web.PageInspector.Loader

    未能加载文件或程序集"Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, P ...

  9. ping主机脚本

    #!/bin/bash #ping net='172.16.1' uphosts=0 downhosts=0 for i in {1..254};do ping -c 1 -w 1 ${net}.${ ...

  10. mysql用户与权限管理笔记

    今天想使用一下李刚那本书上的hibernate的Demo,试出了点问题,过程中就发现mysql的用户管理和权限管理上也有点东西要注意,所以顺便就写一下mysql用户管理和权限管理的笔记. 先说一说my ...