[Leetcode] Roman to integer 罗马数字转成整数
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路:有关罗马数字的相关知识可见博客Integer to roman。从左往右遍历字符串,比较当前为和下一位的值,若是当前数字比下一位大,或者当当前数字为最后时,则加上当前数字,否则减去当前数字。这就遇到一个问题,罗马数字的字符串不是按26个字母顺序来的,如何确定大小。解决办法一:写一个函数,将罗马数字转换成整数,然后进行比较;解法二,可以使用map数据结构,将罗马数字的字母转换成对应的整数值。
方法一的代码如下:
class Solution {
public:
int romanToInt(string s)
{
int res=; //记得初始化
for(int i=;i<s.size();++i)
{
if(i==s.size()-||strToNum(s[i])>=strToNum(s[i+]))
res+=strToNum(s[i]);
else
res-=strToNum(s[i]);
}
return res;
}
int strToNum(const char c)
{
int num=;
switch(c)
{
case 'M':
num=;
break;
case 'D':
num=;
break;
case 'C':
num=;
break;
case 'L':
num=;
break;
case 'X':
num=;
break;
case 'V':
num=;
break;
case 'I':
num=;
break;
default:
num=;
}
return num;
}
};
方法二:
class Solution {
public:
int romanToInt(string s) {
int res = ;
unordered_map<char, int> m{{'I', }, {'V', }, {'X', }, {'L', }, {'C', }, {'D', }, {'M', }};
for (int i = ; i < s.size(); ++i)
{
if (i == s.size() - || m[s[i]] >= m[s[i+]])
res += m[s[i]];
else
res -= m[s[i]];
}
return res;
}
};
[Leetcode] Roman to integer 罗马数字转成整数的更多相关文章
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- [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 ...
- Roman to Integer(将罗马数字转成整数)
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】Roman to Integer(罗马数字转整数)
这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...
- 013 Roman to Integer 罗马数字转整数
给定一个罗马数字,将其转换成整数. 返回的结果要求在 1 到 3999 的范围内. 详见:https://leetcode.com/problems/roman-to-integer/descript ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)
1.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range f ...
- LeetCode: Roman to Integer 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
随机推荐
- grafana使用Prometheus数据源监控mongo数据库
数据库改用mongo后,监控需求就需要整合进grafana里,由于一直在坚持docker化部署,那么此次也不例外. 1. 安装Prometheus: What is Prometheus? Prome ...
- laravel构造函数跳转失败
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Requests;us ...
- 8.2 USB键盘驱动编写和测试
目标:根据USB驱动分析和上节的USB鼠标驱动,编写键盘驱动,并测试. 一.原理分析 1. 首先通过打印usb_buf[i]中的8字节数据,看一下按键按下之后会接收到什么. 1)通过按完所有键盘按键打 ...
- PADS快捷键
问:在pads layout中怎样显示或隐藏铺铜的效果 答: 无模命令:po 或者spo 前者是平面层 后者是混合层. 同时你可以在ctrl+alt+c 色彩项中关闭 copper . 使用 无模命令 ...
- JAVA反射之 Field (属性)
主要方法: public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName ...
- Kubernetes-DNS
Kubernetes提供的虚拟DNS服务名为skydns,由四个组件组成: etcd:DNS存储 kube2sky:将Kubernetes Master中的Service(服务)注册到etcd sky ...
- ccf201703-2 STLlist
题目:http://118.190.20.162/view.page?gpid=T56 问题描述 体育老师小明要将自己班上的学生按顺序排队.他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面, ...
- dfs Gym - 100989L
AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...
- WPF中的命令与命令绑定(二)
原文:WPF中的命令与命令绑定(二) WPF中的命令与命令绑定(二) 周银辉在WPF中,命令(Commandi ...
- 【数据库】 SQL 常用语句
[数据库] SQL 常用语句 1.批量导入 INSERT INTO Table2(field1,field2,...) SELECT value1,value2,... FROMTable1 要求目标 ...