题目:

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. 必须会的SQL语句(八)数据库的完整性约束

    实体完整性 1.建表时定义主键   Create table 表名    (         Sno int identity(1,1),         Sname nvarchar(20),    ...

  2. Ubuntu VPN连接设置

    右击面板上的网络图标->VPN连接->配置VPN 点击“添加” 选择默认的PPTP VPN连接类型,点击“建立” 连接名称随便取一个.填入你到VPN网关和用户名.密码 点击“高级”,在“允 ...

  3. datagridview添加复选框全选和取消

    全选 private void All_selected_Click(object sender, EventArgs e) { ; i < this.DataGridViewProduct.R ...

  4. JavaScript计算日期间隔以及结果错误(少一天)的解决方法

    下面的代码是之前从网上某个地方COPY下来的,之前一直用着,前段时间DateDiff()方法突然出问题了,输入两个日期2015-10-01 和 2015-10-02之后,计算出来的日期是0!如果只有几 ...

  5. Delphi XE5 for android 使用 BITMAP STYLE DESIGNER 改变控件背景

    一.BITMAP STYLE DESIGNER 工具集成在IDE开发工具的TOOLS菜单. 使用NEW 新建一个安卓样式.NEW—NEW ANDROID STYLE FOR FIREMONKEY. 这 ...

  6. C# 常用的dialogresult reset 以及if else 等检查获取客户操作信息的操作方法

    DialogResult reset; reset= MessageBox.Show("请检查您的输入信息是否按照规则输入的", "信息输入好像有问题哦", M ...

  7. scala构造器实战

    父类 abstract class Event(val name:String) { var time:Long var content:String } 子类 private[spark] clas ...

  8. (转)Linux下用mkisofs制作光盘镜像ISO文件

    我们都知道在windows下有winiso可以将光盘制作成光盘镜像ISO文件,在linux下一个命令就搞定了.那就是mkisofs.先看看mkisofs的help. rory@dev:~$ mkiso ...

  9. UIViewAnimationOptions swift 2

    UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, ...

  10. oracle 11g 表空间使用率

    Oracle数据库表空间使用量查询: select b.file_name 物理文件名,b.tablespace_name 表空间,b.bytes/1024/1024 大小M,(b.bytes-sum ...