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. 一些基于jQuery开发的控件

    基于jQuery开发,非常简单的水平方向折叠控件.主页:http://letmehaveblog.blogspot.com/2007/10/haccordion-simple-horizontal-a ...

  2. Nginx+Tomcat搭建高性能负载均衡集群--Windows本地测试版

    链接:http://www.cnblogs.com/wbyp/p/6860744.html

  3. 【BZOJ4006】管道连接(动态规划,斯坦纳树)

    题面 BZOJ 洛谷 题解 和这题区别不是很大吧. 基本上拿过来改一下就做完了. #include<iostream> #include<cstdio> #include< ...

  4. python之插入排序

    插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2).是稳定的排序方法.插入算法把要排序的数组分成两部 ...

  5. docker虚拟机动态扩展内存

    需求:将一台docker虚拟机的内存从6G扩展到8G. 关于资源限制,docker使用的是cgroup,这里就不细说原理了,只记录一下操作方法. 1.先找到容器ID # docker ps 2.进入对 ...

  6. Android Studio 换主题 + 背景图片 + 去掉白色竖线

    1.去掉AS编辑区域右边的白色竖线: 把right margin 设置的大一点就可以了,默认是120 ,设置成 1200就ok了 2.AS主题下载换装 可以去如下网站下载,然后导入jar, 具体用法百 ...

  7. Using Immutable in React + React-Redux

    React-Redux Introduction React-Redux is a library for React based on Redux package. And the core ide ...

  8. git 利用hook 实现服务器自动更新代码

    如何利用git的hook实现提交代码后自动更新? 因为个人开发经常需要提交代码,每次都需要连接服务器去pull代码,重启服务器就显得十分繁琐,因此github提供了一个时间钩子,用户push代码后可以 ...

  9. O(1)时间复杂度求栈中最小元素

    import java.util.Stack; /** * 功能:O(1)时间复杂度求栈中最小元素 * 思路:空间换取时间,使用两个栈,stack1栈存储数据,stack2栈存储最小值: * stac ...

  10. python基础之map/reduce/filter/sorted

    ---map(fun,iterable) 首先来看一看map()函数,map函数接受两个参数,第一个参数是函数的名称,第二个参数一个可迭代对象.即map(fun,iterable) map函数就是将具 ...