Integer to Roman

Given an integer, convert it to a roman numeral.

The number is guaranteed to be within the range from 1 to 3999.

Example

4 -> IV

12 -> XII

21 -> XXI

99 -> XCIX

more examples at: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

分析:

在罗马数字里,除了1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 ,其它数字都可以由以上数字“拼接”而成。以上数字的罗马字符为:

"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" 。

所以,对于15,我们找出10和5,放在一起就是XV,16就是10+5+1, XVI(这里一定是从大到小,能够取最大就必须取最大,不能是 10 + 4 + 1 + 1).

 public class Solution {
/**
* @param n
* The integer
* @return Roman representation
*/ public String intToRoman(int number) {
int[] values = { , , , , , , , , , , , , };
String[] numerals = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
StringBuilder result = new StringBuilder();
for (int i = ; i < values.length; i++) {
while (number >= values[i]) {
number -= values[i];
result.append(numerals[i]);
}
}
return result.toString();
}
}

Roman to Integer

Given a roman numeral, convert it to an integer.

The answer is guaranteed to be within the range from 1 to 3999.

Example

IV -> 4

XII -> 12

XXI -> 21

XCIX -> 99

 public class Solution {
/**
* @param s Roman representation
* @return an integer
*/
public int romanToInt(String s) {
if(s.length() < ) return ;
int value = ;
int pValue = getRomanValue(s.charAt()); // the value of previous character for (int i = ; i < s.length(); i++) {
int cValue = getRomanValue(s.charAt(i));
if (pValue >= cValue) {
value += pValue;
} else {
value -= pValue;
}
pValue = cValue;
}
value += pValue; // we always add the last value to value. No exception.
return value; }
public int getRomanValue(char c) {
switch(c) {
case 'I': return ;
case 'V': return ;
case 'X': return ;
case 'L': return ;
case 'C': return ;
case 'D': return ;
case 'M': return ;
default: return ;
}
}
}

Integer to Roman & Roman to Integer的更多相关文章

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

  2. 5.Integer to Roman && Roman to Integer

    Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...

  3. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

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

  4. Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?

    Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么? Integer.valueof(String s)是将一个包装类是将一个实际 ...

  5. java中Integer,String判断相等与integer的比较大小

    package sfk.bbs.test.springjsbctempletTest; import static org.junit.Assert.*; import org.junit.Test; ...

  6. Integer.valueOf()与Integer.parseInt()区别

    Integer.parseInt()和Integer.valueOf()有本质区别,具体如下列: Integer.parseInt()把String   型转换为Int型,  Integer.valu ...

  7. Java 的Integer、int与new Integer到底怎么回事?

    先做一些总结,询问了些经验比较多的师傅,在这里表示感谢,然后自己总结下,今天的收获分享给大家: 1. int 和Integer在进行比较的时候,Integer会进行拆箱,转为int值与int进行比较. ...

  8. Java面试必看之Integer.parseInt()与Integer.valueOf()

    Integer.parseInt()和Integer.valueOf()都是将成为String转换为Int,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深挖 ...

  9. 深挖的Java源代码之Integer.parseInt()vs Integer.valueOf()

    Integer.parseInt()和Integer.valueOf()都是用来将String转换为Int的,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深 ...

随机推荐

  1. Powerful array CodeForces - 86D(莫队)

    给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和.1<=n,m<=200000.1<=s<=10^6 #include <iostr ...

  2. Paint Chain HDU - 3980(sg)

    因为题中是个环, 所以我们可以首先拿出一组m 如果n<m 先手必输 否则的话跑sg函数 n = n-m #include <iostream> #include <cstdio ...

  3. Luogu 1429 平面最近点对 | 平面分治

    Luogu 1429 平面最近点对 题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 ...

  4. java解数独

    先输入要解的数独,采用多维数组来保存其中的值,未填数字的地方,初始化为0,然后采用递归的方法来解数独. 直接上代码: /** * * @author walker * */ public class ...

  5. Linux crontab 命令格式与举例

    每五分钟执行  */5 * * * * 每小时执行     0 * * * * 每天执行        0 0 * * * 每周执行       0 0 * * 0 每月执行        0 0 1 ...

  6. 一、linux学习之centOS系统安装(VMware下安装)

    一.下载 这个真的没有什么技术含量,也不附下载连接了.这里需要说明的是,其实在VMware下安装centOS是非常简单的,但是这里我要纪录的是在PC上安装centOS,之所以跟标题有出入是因为为了纪录 ...

  7. T48566 【zzy】yyy点餐

    T48566 [zzy]yyy点餐 题目描述 yyy去麦肯士吃垃圾食品. 麦肯士有n种单点餐品(汉堡薯条鸡翅之类的).每次选择一种或者以上的餐点,且每种餐点不多于一个的话,可以认为是购买套餐.购买一个 ...

  8. Hadoop基础-HDFS数据清理过程之校验过程代码分析

    Hadoop基础-HDFS数据清理过程之校验过程代码分析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想称为一名高级大数据开发工程师,不但需要了解hadoop内部的运行机制,还需 ...

  9. Hadoop基础-HDFS分布式文件系统的存储

    Hadoop基础-HDFS分布式文件系统的存储 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HDFS数据块 1>.磁盘中的数据块 每个磁盘都有默认的数据块大小,这个磁盘 ...

  10. android中service启动后台程序

    Service是Android中一个类,它是Android四大组件之一,使用Service可以在后台执行长时间的操作( perform long-running operations in the b ...