【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 ...
随机推荐
- 实例化新的一个(new)
今天越到了一个特别尴尬的问题,其实特别简单就一句代码的事. PlayList pModel = new PlayList(); foreach (XmlNode xn1 in xnl) { ····· ...
- jquery用ajax方式从后台获取json数据,将内容填充到下拉列表。
从后台获取json数据,将内容填充到下拉列表. url:链接 par:ID sel:下拉列表选择器 //获取下拉列表 function BuildSelectBox(url, par, sel) { ...
- 将List<T>转化成 DataTable--调整可空类型的转化错误
加载表结构并保持成XML string cmdText = @"select * from kb_lable_temp where 1=2"; using (SqlConnecti ...
- firefox 中碰到的一个小坑
情况描述: 在一个处于正常文档流的div中,里面有一部分文字,还有个有浮动的块, 上代码 HTML: <div class="container"> this is ...
- c语言学习的第6天
#include <stdio.h> int main() { int x=100; if(x==0) { printf("x等于0\n"); printf(" ...
- linux分区和文件系统
linux分区主分区:最多只能有4个扩展分区:最多只能有一个 主分区+扩展分区最多4个 扩展分区不能写入数据,只能包含逻辑分区 见图示:fq.png 主分区:总共最多只能分4个扩展分区:只能有1个,也 ...
- 3)Java容器
3)Java容器 Java的集合框架核心主要有三种:List.Set和Map.这里的 Collection.List.Set和Map都是接口(Interface). List lst = new ...
- Delphi初学者应小心的六大陷阱
Delphi初学者应小心的六大陷阱 作者:子夜编译 初学DelphiI的人,由于各种原因,对DelphiI中的许多概念不能很好的理解,并由此带来了许多的问题,或者是开发出的程序稳性不好 ...
- 多分类问题multicalss classification
多分类问题:有N个类别C1,C2,...,Cn,多分类学习的基本思路是"拆解法",即将多分类任务拆分为若干个而分类任务求解,最经典的拆分策略是:"一对一",&q ...
- ok6410的madplay配置
二.移植嵌入式播放器 madplay madplay 播放器程序主要依赖于如下库: zlib zlib-1.1.4.tar.gz 提供数据压缩用的函式库 libid3tag libid3tag- ...