一天一道LeetCode系列

(一)题目

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

(二)解题

和上一题相反,这题将罗马数字转换成整形数。

注意到 4,9,40,90,400,900这些特殊的数字的区别就不难写出代码了。

class Solution {
public:
    int romanToInt(string s) {
        int ret=0;
         for(auto iter = s.begin() ; iter!=s.end() ; ++iter)
         {
             if((iter+1)!=s.end() && tonum(*iter) < tonum(*(iter+1)))
                {
                    ret += tonum(*(iter+1))-tonum(*iter);
                    ++iter;
                }
             else
                ret += tonum(*iter);
         }
         return ret;
    }

    int tonum(char ch)
    {
        switch(ch)
        {
            case 'I' : return 1;
            case 'V' : return 5;
            case 'X' : return 10;
            case 'L' : return 50;
            case 'C' : return 100;
            case 'D' : return 500;
            case 'M' : return 1000;
        }
    }

};

【一天一道LeetCode】#13. Roman to Integer的更多相关文章

  1. 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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

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

  3. [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 ...

  4. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

    1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  5. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

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

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

  7. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  8. [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 ...

  9. [LeetCode] 13. Roman to Integer ☆☆

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  10. [leetcode] 13. Roman to Integer

    如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...

随机推荐

  1. 2017京东校招面试回忆(已成功拿到offer)

    一面 24日 晚上5:30-6:40 1 先说自己熟悉的领域 2 list的实现有什么?   arraylist1.6 1.7区别  底层   linkedlist 底层是怎么实现的 单向还是双向   ...

  2. Android makefile编写基础

    首先来看一个简单的Android makefile,这个是我上篇文章写的,重新摘出来: LOCAL_PATH:=$(call my-dir) include $(CLEAR_VARS) LOCAL_M ...

  3. android开发常用工具箱

    我的工具包资料目录 我的个人总结,最近做的项目需要了的一些资料,感觉挺乱的,然后现在整理了一下. Jar包 包名 版本号 作用 下载地址 xUtils 2.6.14和3.1.26 大文件上传下载等 旧 ...

  4. Git运用基础之如何删除Github上不想要的项目

    今天突然想删除,(强迫症想删除)之前练习时多创建的多个Github上的源代码或者无用Demo地址,然后看了一些文章都比较老式,这里我展示一下最新的删除步骤. 一.首先登录自己的Github账户主页(没 ...

  5. 剑指Offer——完美+今日头条笔试题+知识点总结

    剑指Offer--完美+今日头条笔试题+知识点总结 情景回顾 时间:2016.9.28 16:00-18:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:完美世界笔试 ...

  6. EJB开发第一个无状态会话bean、开发EJB客户端

    开发第一个无状态会话bean EJB中的三中bean: 会话Bean(Session Bean) 负责与客户端交互,是编写业务逻辑的地方,在会话bean中可以通过JDBC直接操作数据库,但大多数情况下 ...

  7. svn(subversion)代码版本管理在linux下的一些常见使用命令

    以下的操作都是默认你的服务器安装有svn的大前提下进行的. 一.创建版本库 我的版本库存放路径为: /var/svn : 下面我们来创建一个名为 svntet 的版本库    注释: svnadmin ...

  8. 使用IDEA运行Spark程序

    使用IDEA运行Spark程序 1.安装IDEA 从IDEA官网下载Community版本,解压到/usr/local/idea目录下. tar –xzf ideaIC-13.1.4b.tar.gz ...

  9. 【ShaderToy】跳动的心❤️

    写在前面 注:如果你还不了解ShaderToy,请看开篇. 作为ShaderToy系列的第一篇,我们先来点简单的.下面是效果: (CSDN目前不能传gif文件了,暂时空缺,可以看下面的原shader效 ...

  10. 解析JSON的三种方式

    JSONObject   JSONObject jsonObject = new JSONObject(strJson); JSONArray jsonArray = jsonObject.getJS ...