【Roman To Integer】cpp
题目:
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的更多相关文章
- LeetCodeOJ刷题之13【Roman to Integer】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【Reverse Integer】cpp
题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click ...
- 【First Missing Positive】cpp
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 【Merge Sorted Array】cpp
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
- 【Count and Say】cpp
题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- 【Power of Two】cpp
题目: Given an integer, write a function to determine if it is a power of two. 代码: class Solution { pu ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【Search Insert Position 】cpp
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
随机推荐
- 极速地将git项目部署到SAE的svn服务器上
本文最初发布于我的个人博客:http://jerryzou.com/posts/gitForSAE/ 我花了一些时间自己写了一个能够极速地将一个git项目部署到SAE的svn服务器上的脚本.代码不是复 ...
- 空间session失效的解决方法
今天访问自己的网站的时候(by thinkphp),突然发现身份验证失效了,Session无法跨页,而且登陆的时候总是提示验证码错误(验证码也是通过Session传递的),才意识到可能是Session ...
- Objective-C数据类型、数据类型转换
数据类型 1.Objective-C数据类型可以分为:基本数据类型.对象数据类型和id类型. 2.基本数据类型有:int.float.double和char类型. 3.对象类型就是类或协议所声明的指针 ...
- SQL中迁移sql用户及密码脚本
SQL中迁移sql用户及密码脚本 编写人:CC阿爸 2014-6-20 在日常SQL数据库的操作中,常常需要迁移数据库或重装服务器,这时候,一些之前建立的login账户,必须重新建立,以下可以通过 ...
- win7防火墙打不开(无法启动windows firewall服务)
点击windows 7控制面板中防火墙的“推荐配置”没有反应:打开“服务”,无法启动windows firewall,并报错. 可能很多的win7用户都碰到过这样的一种情况,那就是win7的防火墙打 ...
- 百度搜索API v3版本与soap
技术文档请参考http://dev2.baidu.com/docs.do?product=2#page=File,和http://dev2.baidu.com/docs.do?product=2#pa ...
- [.NET 4.5] ADO.NET / ASP.NET 使用 Async 和 Await 异步 存取数据库
此为文章备份,原文出处(我的网站) [.NET 4.5] ADO.NET / ASP.NET 使用 Async 和 Await 异步 存取数据库 http://www.dotblogs.com.tw ...
- asp.net中两款文本编辑器NicEdit和Kindeditor
过Web开发的朋友相信都使用过富文本编辑器,比较出名的CuteEditor和CKEditor很多人应该已经使用过,在功能强大的同时需要加载的东西也变得很多.下面要推荐的两款富文本编辑器都是使用JS编写 ...
- UICollectionView [NSIndexPath section]: message sent to deallocated instance
在UICollectionView上加UITapGestureRecognizer手势时,点击哪都报 [NSIndexPath section]: message sent to deallocate ...
- CentOS6.4上搭建hadoop-2.4.0集群
公司Commerce Cloud平台上提供申请主机的服务.昨天试了下,申请了3台机器,搭了个hadoop环境.以下是机器的一些配置: emi-centos-6.4-x86_64medium | 6GB ...