题目:

Given a roman numeral, convert it to an integer.

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

代码:

class Solution {
public:
int romanToInt(string s) {
const int size = ;
std::map<char, int> symbol_value;
char symbol_ori[size] = {'M','D','C','L','X','V','I'};
int value_ori[size] = {,,,,,,};
for ( size_t i = ; i < size; ++i )
symbol_value[symbol_ori[i]]=value_ori[i]; int result = symbol_value[s[]];
for ( size_t i = ; i < s.size(); ++i )
{
if ( symbol_value[s[i]] > symbol_value[s[i-]] )
{
result += symbol_value[s[i]] - *symbol_value[s[i-]];
}
else
{
result += symbol_value[s[i]];
}
}
return result;
}
};

tips:

根据Roman数字的构造规则:

如果当前的symbol比前一个symbol大,则当前symbol的值减去二倍之前的那个值,再累加到result中。

(为什么要减2倍,因为这个值之前已经被累加一遍了,所以减去2倍就正好了)

===============================================

第二次过这道题,采用了跟第一次不一样的算法,完全跟着感觉走了。如果前一个比后一个小,那么就直接把后一个处理了,跳过去。

另外结尾的时候判断一下最后一个元素是不是被之前的那个吃掉了(即倒数第二个元素比倒数第一个小)。

class Solution {
public:
int romanToInt(string s) {
map<char,int> symbol_value;
symbol_value['M'] = ;
symbol_value['D'] = ;
symbol_value['C'] = ;
symbol_value['L'] = ;
symbol_value['X'] = ;
symbol_value['V'] = ;
symbol_value['I'] = ;
int ret = ;
int i=;
for ( ;i<s.size()-; ++i )
{
if( symbol_value[s[i]] < symbol_value[s[i+]] )
{
ret = ret + symbol_value[s[i+]] - symbol_value[s[i]];
++i;
}
else
{
ret = ret + symbol_value[s[i]];
}
}
if ( i==s.size()- ) ret += symbol_value[s[i]];
return ret;
}
};

【Roman To Integer】cpp的更多相关文章

  1. LeetCodeOJ刷题之13【Roman to Integer】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  2. 【Reverse Integer】cpp

    题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click ...

  3. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  4. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  5. 【Count and Say】cpp

    题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...

  6. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  7. 【Power of Two】cpp

    题目: Given an integer, write a function to determine if it is a power of two. 代码: class Solution { pu ...

  8. 【Spiral Matrix II】cpp

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  9. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

随机推荐

  1. 淮安团购网美团联盟网赚版 v5.7

    淮安团购网,主要是利用美团联盟的hao123版API大家可以注册http://union.meituan.com获取api 核心采用dede5.7所以在安装上没有大的问题,安装好后后台恢复备份就可以了 ...

  2. 快速清理Visual Studio起始页最近打开项目

    清除vs2008起始页最近打开项目 第一种:最简单的方式: 把以下内容保存为.bat批处理文件 @echo off@REG Delete HKCU\Software\Microsoft\VisualS ...

  3. K-Means(K均值)算法

    昨晚在脑内推导了一晚上的概率公式,没推导出来,今早师姐三言两语说用K-Means解决,太桑心了,昨晚一晚上没睡好. 小笨鸟要努力啊,K-Means,最简单的聚类算法,好好实现一下. 思路: 共有M个样 ...

  4. 安装elasticsearch

    安装elasticsearch   来自:http://www.cnblogs.com/huangfox/p/3541300.html 一)安装elasticsearch 1)下载elasticsea ...

  5. mysql中character_set_connection的作用

    character_set_client = x character_set_results = xcharacter_set_connection = x; 我们常用在mysql操作类中使用这三面, ...

  6. 《安全参考》HACKCTO-201312-12

    小编的话 “忽如一夜春风来,千树万树梨花开.” 小伙伴们,不要只为了“千树万树的梨花”而惊喜,陶醉! 与此同时,您最爱的整合型信息安全技术期刊<安全参考>第12期也如约而至啦! 这一期&l ...

  7. CodeBlocks集成cppcheck

    From:http://www.cnblogs.com/killerlegend/p/3624117.html Writer:KillerLegend CodeBlocks本身配置了cppcheck的 ...

  8. 关于Raw,Assets的使用

    Raw,Assets下文件区别: 相同点:两个目录下的文件在打包后都会原封不动的保存到apk中,不会被编译成二进制. 不同点:Raw下文件不能使用目录结构, 有些格式的会被压缩,能够通过R.raw方便 ...

  9. R语言的日期运算

    写hive SQL查询, 需要从导入的参数, 自动累加日期. 从而实现一个自动的,多个日期的统计过程 R语言的日期运算超级简单. > test<-Sys.Date() > test ...

  10. 对ASP.NET Entity FrameWork进行单元测试

    添加一个测试用的类库:将Web.config中的connectionstrings节点下的东东复制一份到刚添加的类库的app.config下 使用NUint+TestDriven.net进行测试: 如 ...