leetcode Roman Integer
class Solution {
public:
int romanToInt(string s) {
if (s.length() < 1)
return 0;
map<char,int> m;
m['I'] = 1;
m['V'] = 5;
m['X'] = 10;
m['L'] = 50;
m['C'] = 100;
m['D'] = 500;
m['M'] = 1000;
int i = s.length() - 1;
int sum = m[s[i--]];
while (i >= 0) {
if (m[s[i + 1]] > m[s[i]])
sum -= m[s[i]];
else
sum += m[s[i]];
--i;
}
return sum;
}
};
class Solution {
public:
string intToRoman(int num) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string res = "";
int i = 0;
while (num > 0) {
if (num >= value[i]) {
res += symbol[i];
num -= value[i];
}
else
++i;
}
return res;
}
};
leetcode Roman 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 ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [Leetcode] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- 【JAVA、C++】LeetCode 012 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- 我现所认知的SSH
实习了三个月,对着SSH有着一定的认识了,就以自已认识的大概思路写一篇文章吧,留给以后的自已,也恳请各位博友们如果看到我的认识有过错的地方能帮我指正过来! 我用到的资料 在写正文之前,先说说我这段时间 ...
- 简单实现一个EventEmiter
在前端开发中,“发布-订阅”也是“观察者模式”是一种常用的设计模式:之前对设计模式没有过深的认识,直到前段时间在封装一个运用AngularJS封装table组件时,遇到一个难题,那就是AngularJ ...
- Web大前端面试题-Day2
1.伪类与伪元素的区别? 1) 定义区别 伪类 伪类用于选择DOM树之外的信息,或是不能用简单选择器进行表示的信息. 前者包含那些匹配指定状态的元素,比如:visited,:active:后者包含那些 ...
- BZOJ.2668.[CQOI2012]交换棋子(费用流zkw)
题目链接 首先黑白棋子的交换等价于黑棋子在白格子图上移动,都到达指定位置. 在这假设我们知道这题用网络流做. 那么黑棋到指定位置就是一条路径,考虑怎么用流模拟出这条路径. 我们发现除了路径的起点和终点 ...
- HDU5420 : Victor and Proposition
以深度建立线段树,线段树父亲节点向儿子节点连边,然后用线段树合并可以得到任何一个点子树的线段树,只需向对应节点的线段树中的$O(\log n)$个点连边即可.为了保证连边关系不发生混乱,线段树需要进行 ...
- bzoj 3285 离散对数解指数方程
/************************************************************** Problem: 3285 User: idy002 Language: ...
- Linux学习笔记05—文件与目录权限
1. 绝对路径与相对路径绝对路径:路径的写法一定由根目录 ‘/’写起,例如 /usr/local/mysql 这就是绝对路径相对路径:路径的写法不是由根目录 ‘/’写起,例如:首先用户进入到/, 然后 ...
- STM32F4xx -- Cortex M4
STM32F4xx official page: http://www.st.com/internet/mcu/subclass/1521.jspIntroductionFPU - Floating ...
- A brief CRC tutorial
https://www.kernel.org/doc/Documentation/crc32.txt A brief CRC tutorial. A CRC is a long-division re ...
- bitnami-redmine邮件告警配置
配置 bitnami-redmine的配置文件与单纯的redmine配置文件可能并不相同,在这里我们需要打开一下配置文件: /opt/bitnami/apps/redmine/htdocs/confi ...