题目:

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. Js图片滚动

    参考博文:http://blog.chinaunix.net/uid-12304670-id-2947067.html <%@ Page Title="" Language= ...

  2. (一)、NodeJS (转载)

    NodeJS基础                                                 ------ 转载自阿里巴巴 什么是NodeJS JS是脚本语言,脚本语言都需要一个解 ...

  3. CentOS 7服务

    重启防火墙service firewalld start/restart/stop 使用systemctl来启动/停止/重启服务要启动一个服务,你需要使用如下命令:# systemctl start ...

  4. Windows 和  Linux 下 禁止ping的方法

    Windows 和Linux 下 禁止ping的方法 目的: 禁止网络上的其他主机或服务器ping自己的服务器 运行环境: Windows 03.08  linux 方法: Windows 03下: ...

  5. C#中的委托、事件和设计模式(转载)

    引言 委托和事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人 ...

  6. 9.css背景

    这节也是关于盒模型的扩展,而我个人喜欢把盒模型想象成图画.元素的尺寸调整就是画布大小的调整,边框的设置就是画框的镶嵌.但是,作为一个最终要将画作展现到美术馆(网页)的艺术家来说,仅仅是这样还是不够的, ...

  7. 线程操作API

    线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...

  8. [转]关于VC预定义常量_WIN32,WIN32,_WIN64

      VC2012 下写 Windows 程序时,有时需要判断编译环境.在之前的文章<判断程序是否运行在 Windows x64 系统下.>里说过如何在运行期间判断系统环境,但在编译时如何判 ...

  9. 基于opencv 的图片模糊判断代码

    #include"cv.h"  #include"highgui.h"  #include<iostream>  using namespace s ...

  10. SqlBulkCopy与触发器,批量插入表(存在则更新,不存在则插入)

    临时表:Test /****** 对象: Table [dbo].[Test] 脚本日期: 05/10/2013 11:42:07 ******/ SET ANSI_NULLS ON GO SET Q ...