Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

解析:只有数字为 9 和 4 时,取一个小数放在左边。(把数字为 9 和 4 时的罗马符号看作一个原子,避免取小数过程。)

 class Solution {
public:
string intToRoman(int num) {
string Roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int val[] = {, , , , , , , , , , , , };
string s;
for(int i = ; i < ; ++i){
while(num >= val[i]){
num -= val[i];
s += Roman[i];
}
}
return s;
}
};

 Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

解析:1. 最直观想法,对字符串按罗马值从大到小比对。 (372 ms)

 bool inHead(string s1, string& s2)
{
if(s2 == "") return false;
int k = ;
auto it = s1.begin();
for(; it != s1.end(); ++it)
{
if(*it != s2[k++]) return false;
}
if(it == s1.end()){
s2.erase(, k);
return true;
}
} class Solution {
public:
int romanToInt(string s) {
string sigmal[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int a[] = {, , , , , , , , , , , , };
int num = ;
int k = ;
for(int i = ; i < ; ++i){
while(inHead(sigmal[i], s)){
num += a[i];
}
}
return num;
}
};

Code

2. 利用 hash 函数来做。(每个罗马字符范围都在 'A' - 'Z' 之间,对字符串从左到右扫描,若是出现逆序(按罗马值),则这个逆序对为一个值) (260ms)

 class Solution {
public:
int romanToInt(string s) {
char c[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'};
int v[] = {, , , , , , };
int hash[] = {};
for(int i = ; i < ; ++i){
hash[c[i]] = v[i];
}
int num = ;
int len = s.length();
for(int i = ; i < len; ++i){
if(i == len-){
num += hash[s[i]];
return num;
}
if(hash[s[i]] < hash[s[i+]]){
num += hash[s[i+]] - hash[s[i]];
++i;
}else num += hash[s[i]];
}
return num;
}
};

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

  3. Integer to Roman & Roman to Integer

    Integer to Roman Given an integer, convert it to a roman numeral. The number 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. android baseActivity

    package newdemo.jeno.designdemo.activitynew; import android.os.Bundle;import android.support.annotat ...

  2. For-Each循环

    For-Each循环也叫增强型的for循环,或者叫foreach循环. For-Each循环是JDK5.0的新特性(其他新特性比如泛型.自动装箱等). For-Each循环的加入简化了集合的遍历. 语 ...

  3. Enable SSHD on Ubuntu

    https://help.ubuntu.com/community/SSH/OpenSSH/Configuring

  4. Psp个人软件开发软件需求分析及用例分析

    一.需求分析 1.  业务需求 1.1 应用背景 开发项目进度计划总是那么不明确,延期经常出现,甚至无法给出一个相对比较明确的延迟时间.这样给市场的推广会带来很大的影响,不确定因素使得应对十分困难. ...

  5. 如何在Kali Linux中搭建钓鱼热点

    文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用! 0×00 实验环境 操作系统:Kali 1.0 (VM) FackAP: easy-creds 硬件:NETGEAR wg ...

  6. import和from import陷阱二

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 #from os import path import os.path path='/home/vamei/doc/file.txt' ...

  7. Server ran out of threads to serve requests. Consider raising the ThreadsPerChild setting

    最近每天半夜,服务器都会出现崩掉的现象,pc  app 都不能正常使用 查看错误日志发现问题所在: [Wed Nov 09 08:07:28.651642 2016] [mpm_winnt:error ...

  8. .jre下的lib和jdk下的lib的区别

    jre是JDK的一个子集.提供一个运行环境.JDK的lib目录是给JDK用的,例如JDK下有一些工具,可能要用该目录中的文件.例如,编译器等.JRE的lib目录是为JVM,运行时候用的.包括所有的标准 ...

  9. turn.js 图书翻页效果

    今天用turn.js 做图书的翻页效果遇到问题: 图片路径总是出错 调了一天,总算调出来了 我用的thinkphp,其他的不知道是不是一样 三 个地方要改动: 1.后台查出地址 注意的地方:1.地址要 ...

  10. codeForce-589D Boulevard(判断线段是否相交)

    题目大意:n个人.一个区间.每个人都会在某个时间段内按相同的速度(所有人的速度都一样,都是1或-1)在他的区间内从一个端点走到另一个端点(只走一次).问每个人会与几个人碰面. 题目分析:将时间看成一个 ...