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. 对ASIHTTPRequest的封装

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/quanqinayng/article/details/37659751 .h文件  // // Ht ...

  2. SAP 后台job

    SAP 如何定义后台job 有两种方 1是se38执行可执行程序后,菜单栏‘程序’--->'后台执行',输入输出设备,ENTER两次后,选择开始时间(立刻执行,或定义日期时间,也可周期执行..) ...

  3. golang strings.Split的疑问

    先看下面的代码 func main() { fmt.Println("Hello, 世界") cc:=[...]int{} b:="" a:=strings.S ...

  4. 编译hadoop2.6.0 cdh 5.4.5 集成snappy压缩

    原文地址:http://www.cnblogs.com/qiaoyihang/p/6995146.html 自带的为32位库,故需要把64为重编译进去 1.下载源码:http://archive-pr ...

  5. ORACLE 多表连接与子查询

    Oracle表连接 SQL/Oracle使用表连接从多个表中查询数据 语法格式: select 字段列表from table1,table2where table1.column1=table2.co ...

  6. 关于myeclipse+tomcat+struct2的热部署问题

    今天满心欢喜的打开电脑来写程序,却不曾想到它竟然给我搞事情,前几天刚学的struct2热部署竟然不好用了.每次改java文件都要重新部署好麻烦,最后花了了好长时间才解决,必须在这里总结一下: (1)s ...

  7. mybatis 复习笔记02

    1. 一对一查询: 1). 实体类: 2). 定义resultMap <!-- 订单查询关联用户的resultMap 将整个查询的结果映射到cn.itcast.mybatis.po.Orders ...

  8. UILable 的 属性设置

    //UILable的大小自适应实例 UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];//设定位置与大小 [m ...

  9. JSP与Servlet之前台页面自动回复之实现

    [JSP与Servlet之前台页面自动回复之实现] 该内容 来自于imooc的一个视屏教程.http://www.imooc.com/video/4562 就是当点击 发送 的时候把这个对话框内容添加 ...

  10. React-Navigation与Redux整合详解

    本文转自:文章地址:http://blog.csdn.net/u013718120/article/details/72357698 继react-navigation发布已经过去半年的时间,想必Re ...