题目

将罗马数字转换为整数。

解法

可以参考上一篇数字转换为罗马数字的规则。

代码

 class Solution {
public:
int sym2int(char sym)  //罗马数字字符与数字的对应关系
{
switch(sym)
{
case 'I' : return ;
case 'V' : return ;
case 'X' : return ;
case 'L' : return ;
case 'C' : return ;
case 'D' : return ;
case 'M' : return ;
default : return ;
}
} int romanToInt(string s) {
int result = sym2int(s[]);
for(int i = ; i < s.size(); ++i)
{
result += sym2int(s[i]);
if(sym2int(s[i]) > sym2int(s[i-]))  //如果当前字符比上一字符对应的值要大,说明这是一个减的过程,由于上一字符之前计算过,所以减2倍
result -= * sym2int(s[i-]);
}
return result;
}
};

LeetCode题解——Roman to Integer的更多相关文章

  1. [LeetCode 题解]: Roman to Interger

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a ro ...

  2. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

  3. 【LeetCode】Roman to Integer & Integer to Roman

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

  4. Leetcode#13. Roman to Integer(罗马数字转整数)

    题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...

  5. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  6. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  7. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  8. LeetCode 13. Roman to Integer(c语言版)

    题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...

  9. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

随机推荐

  1. lintcode :最长单词

    题目: 最长单词 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", &quo ...

  2. iOS iOS7越狱

    1.使用盘古越狱工具 (或者PP助手) 2.越狱成功后需要安装Apple File Conduit “2”,用于替代afc2add插件 3.安装AppSync插件 (绕过系统验证,随意安装.运行破解的 ...

  3. Android LayoutInflater.from(context).inflate

    在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...

  4. highcharts 结合phantomjs纯后台生成图片系列二之php2

    上篇文章中介绍了phantomjs的使用场景,方法. 本篇文章详细介绍使用php,highcharts 结合phantomjs纯后台生成图片.包含一步步详细的php代码 一.highcharts 结合 ...

  5. HDU 2836 Traversal 简单DP + 树状数组

    题意:给你一个序列,问相邻两数高度差绝对值小于等于H的子序列有多少个. dp[i]表示以i为结尾的子序列有多少,易知状态转移方程为:dp[i] = sum( dp[j] ) + 1;( abs( he ...

  6. tlProPlayer for windows

    tlProPlayer tlProPlayer简介 tlProPlayer是一款定位高性能产品,支持透传,原生输出,并支持硬解码(硬件加速)的多媒体产品,兼容tlplayer所有特性.支持视频加密播放 ...

  7. NuGet学习笔记

    NuGet学习笔记(1)——初识NuGet及快速安装使用 NuGet学习笔记(2)——使用图形化界面打包自己的类库 NuGet学习笔记(3)——搭建属于自己的NuGet服务器

  8. 安卓学习之--如何关闭所有的activity

    根据Activity的声明周期 方法1   我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在A窗口打开B窗口时在Intent中直接加入标志 Intent ...

  9. SPRING STS Virgo OSGI 开发一 : bundle 项目的创建

    1. Spring STS 下载地址  (spring 最近改了站点 暂时不是太熟悉)     http://spring.io/tools/sts/all 2. 下载 Virgo 插件    htt ...

  10. UVa 536 Tree Recovery

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...