一、     题目

将给定的数字(阿拉伯数字)转化成罗马数字。

数字不会大于3999

二、     分析

首先我们要知道神马是罗马数字,尽管听说过。但事实上我还真没有记住,于是就google了下,具体见下:

个位数举例

I, 1 】II, 2】 III, 3】 IV, 4 】V, 5 】VI, 6】 VII, 7】 VIII,8 】IX, 9

十位数举例

X, 10】 XI, 11 】XII, 12】 XIII, 13】 XIV, 14】 XV, 15 】XVI, 16 】XVII, 17 】XVIII, 18】 XIX, 19】 XX, 20】  XXI, 21 】XXII, 22 】XXIX, 29】 XXX, 30】 XXXIV, 34】 XXXV, 35 】XXXIX, 39】 XL, 40】 L, 50 】LI, 51】 LV, 55】 LX, 60】 LXV, 65】 LXXX,
80】 XC, 90 】XCIII, 93】 XCV, 95 】XCVIII, 98】 XCIX, 99 】

百位数举例

C, 100】 CC, 200 】CCC, 300 】CD, 400】 D, 500 】DC,600 】DCC, 700】 DCCC, 800 】CM, 900】 CMXCIX,999】

千位数举例

M, 1000】 MC, 1100 】MCD, 1400 】MD, 1500 】MDC, 1600 】MDCLXVI, 1666】MDCCCLXXXVIII, 1888 】MDCCCXCIX, 1899 】MCM, 1900 】MCMLXXVI,1976】 MCMLXXXIV, 1984】 MCMXC, 1990 】MM, 2000 】MMMCMXCIX, 3999】

正好有我们要求的最大数。

于是我们能够想到,我们仅仅须要将数字与上面的数字相应就可以。

1.     首先。我们要先确定高位即千位的数字,即推断到底是1000还是2000还是3000。于是能够想到每次将num减去1000。并加上一个”M”。

2.     然后,我们推断百位的数字。即能够用num/100来得到百位的数字

3.     再次就是十位和个位,同2的做法

将每个位的字母从左到右连接在一起即构成了我们的结果。

另外,在CSDN上看到一个童鞋的方法非常好

I = 1;

V = 5;

X = 10;

L = 50;

C = 100;

D = 500;

M = 1000;

当中每两个阶段的之间有一个减法的表示,比方900=CM, C写在M前面表示M-C。范围给到3999,情况不多直接打表事实上更快。用代码推断表示预计比較繁琐。

然后就是贪心的做法,每次选择能表示的最大值,把相应的字符串连起来。

class Solution {
public:
string intToRoman(int num) {
string str;
char *hu[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
char *te[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
char *on[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"}; while(num>=1000) {
str+="M";
num-=1000;
}
str += hu[num/100];
num=num%100;
str += te[num/10];
num=num%10;
str+=on[num];
return str;
}
}; 法2:
class Solution {
public:
string intToRoman(int num) {
string str;
string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[]= {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
for(int i=0;num!=0;++i)
{
while(num>=value[i])
{
num-=value[i];
str+=symbol[i];
}
}
return str;
}
}; 法3:JAVA
class Solution {
public:
string intToRoman(int num) {
int[] values = {1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
String[] numerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
StringBuilder result = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (number >= values[i]) {
number -= values[i];
result.append(numerals[i]);
}
}
return result.toString();
}
};

Leetcode:integer_to_roman的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. 【枚举约数】HackerRank - Week of Code 26 - Satisfactory Pairs

    题意:给你一个正整数n,问你存在多少个正整数对a,b(a<b),满足条件:存在正整数x,y,使得ax+by=n. 就预处理出n以内所有数的约数,然后暴力枚举a,暴力枚举x,然后枚举n-ax的所有 ...

  2. Mybatis全部标签

    一.定义SQL语句 (1)select 标签的使用 属性介绍: id :唯一的标识符. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user ...

  3. Problem W: 零起点学算法21——求平均值

    #include<stdio.h> int main() { int a,b,c; scanf("%d %d %d",&a,&b,&c); pr ...

  4. 微软自家的.Net下的JavaScript引擎——ClearScript

    之前我介绍过一个开源的.Net下的Javascript引擎Javascript .NET,今天发现微软自己也开源了一个JavaScript引擎——ClearScript(当然,也支持VB Script ...

  5. mybatis入门基础----动态SQL

    原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 m ...

  6. Error:Execution failed for task ‘:app:processDebugManifest’.

    Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : Attribute ...

  7. java 的 CopyOnWriteArrayList类

    初识CopyOnWriteArrayList 第一次见到CopyOnWriteArrayList,是在研究JDBC的时候,每一个数据库的Driver都是维护在一个CopyOnWriteArrayLis ...

  8. centos7修改yum下载源为阿里源

    在国内很多yum源不好用,所以改成国内的源很有必要 首先,切换到yum源目录 cd /etc/yum.repos.d 备份一下 sudo mv CentOS-Base.repo CentOS-Base ...

  9. postprocessing stack v2

    用了v2和unity2017.3.0f有兼容性问题 在assetbundle的情况下 CopyStd这个shader打不进去 在assetbundle的menafest里面有列但是shader.fin ...

  10. chromatic aberration

    https://github.com/keijiro/KinoFringe https://en.wikipedia.org/wiki/Chromatic_aberration 色差偏移 做神经病效果 ...