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. enmo_day_08

    性能监视 管理内存组件 自动内存管理(AMM) : 指定分配给实例的总内存(SGA, PGA) 自动共享内存管理(ASMM) : 指定SGA, 管理分配给共享池, java池, 动态性能视图 :v$( ...

  2. HDU1004 BALLO0N

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  3. 【转载】【Windows批处理IV】批量进行文件重命名

    1.过滤文件名中所有数字.汉字.特殊字符(含空格) @echo off for %%a in (*.*) do ( if "%%~nxa" neq "%~nx0" ...

  4. 数据结构《16》----自动补齐实现《一》----Trie 树

    1. 简述 Trie 树是一种高效的字符串查找的数据结构.可用于搜索引擎中词频统计,自动补齐等. 在一个Trie 树中插入.查找某个单词的时间复杂度是 O(len), len是单词的长度. 如果采用平 ...

  5. 【转】LokiJS:纯JavaScript实现的轻量级数据库

    原文转自:http://www.html5cn.org/article-7091-1.html LokiJS一个轻量级的面向文档的数据库,由JavaScript实现,性能高于一切.目的是把JavaSc ...

  6. Day20_IO第二天

    1.IO体系总图 2.IO体系--字节流 记忆路线:输入输出流前面加File和Buffered,这样就全记住了 3.表达式解释 表达式:由变量和常量通过运算符连接起来的式子,单个的常量和变量也是表达式 ...

  7. 腾讯优测干货精选|Android双卡双待适配——隐藏在数据库中的那些秘密

    腾讯优测是专业的app自动化测试平台,除了提供兼容性测试,远程真机租用等多维度的测试服务,还有优分享-腾讯内部的移动研发测试干货精选~ 许多APP都希望获取用户通讯录联系人,利用通讯录关系链信息来丰富 ...

  8. libvirt

    http://libvirt.org/index.html libvirt supports: The KVM/QEMU Linux hypervisor The Xen hypervisor on ...

  9. Redis客户端之Spring整合Jedis,ShardedJedisPool集群配置

    Jedis设计 Jedis作为推荐的java语言redis客户端,其抽象封装为三部分: 对象池设计:Pool,JedisPool,GenericObjectPool,BasePoolableObjec ...

  10. 使用Python玩转WMI

    最近在网上搜索Python和WMI相关资料时,发现大部分文章都千篇一律,并且基本上只说了很基础的使用,并未深入说明如何使用WMI.本文打算更进一步,让我们使用Python玩转WMI. 1 什么是WMI ...