博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - 13. Roman to Integer
阅读量:7087 次
发布时间:2019-06-28

本文共 1318 字,大约阅读时间需要 4 分钟。

13. Roman to Integer

 ----------------------------------------------------------------------------

Mean: 

给你一个字符串,代表罗马数字,将其转换为int型数字.

analyse:

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-16-12.06
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <bits/stdc++.h>
using
namespace
std;
typedef
long
long(
LL);
typedef
unsigned
long
long(
ULL);
const
double
eps(
1e-8);
class
Solution
{
public
:
   
int
romanToInt(
string s)
   
{
       
unordered_map
<
char
,
int
>
T
=
       
{
           
{
'I'
,
1  
},
           
{
'V'
,
5  
},
           
{
'X'
,
10  
},
           
{
'L'
,
50  
},
           
{
'C'
,
100
},
           
{
'D'
,
500
},
           
{
'M'
,
1000
}
       
};
       
int
sum
=
T
[s
.
back
()];
       
for (
int
i
= s
.
length()
-
2;
i
>=
0;
--
i)
       
{
           
if (
T
[s
[
i
]]
<
T
[s
[
i
+
1
]])
               
sum
-=
T
[s
[
i
]];
           
else
               
sum
+=
T
[s
[
i
]];
       
}
       
return
sum;
   
}
};
int
main()
{
   
Solution
solution;
   
string s;
   
while(
cin
>>s)
   
{
       
cout
<<
solution
.
romanToInt(s)
<<
endl;
   
}
   
return
0;
}

 

转载于:https://www.cnblogs.com/crazyacking/p/5035757.html

你可能感兴趣的文章
初学UI小知识总结
查看>>
Kotlin--高阶函数
查看>>
多阶段决策问题——DAG(算法竞赛入门经典笔记)
查看>>
UI分析工具YourView开源—App开发者不可多得的利器!
查看>>
记录一次jenkins的部署和使用
查看>>
vscode专题
查看>>
前端基础17:对象/实例/原型
查看>>
tornado 源码之 iostream.py
查看>>
Javascript基础学习干货教程(3)
查看>>
JAVA 泛型理解
查看>>
Git常用命令清单,掌握这些,轻松驾驭版本管理
查看>>
同事说我「变」了
查看>>
Activiti6.0 java项目框架 spring5 SSM 工作流引擎 审批流程
查看>>
SQL 语法速成手册
查看>>
使用nginx控制ElasticSearch访问权限
查看>>
JVM必问知识点:类加载过程
查看>>
Markodwn 标题对齐的同步滚动
查看>>
Flutter 界面路由浅析
查看>>
终端学习记录
查看>>
Python3之递归函数简单示例
查看>>