【Leetcode】【Easy】Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解题:
将字符形式的罗马数字,转化为整形。输入在1~3999之间。罗马数字的书写规范,请参见罗马数字_百度百科;
本题的关键点在于,如何处理I、X、C三个数字放在大数左边是相减,放在大数右边是相加。
解法是,可以从输入字符串的末端开始,从右向左遍历字符串。对于出现的一般罗马字符,进行累加,当出现I、X、C时,判断当前累加值是否达到(>=)5、50、500。如果达到则为相减,如果未达到,则为相加。
原因是,单反需要相减,必定是在大数的左边,因此必定大数已经出现。
class Solution {
public:
int romanToInt(string s) {
int len = s.length();
int sum = ;
for (int i=len-; i>=; --i) {
if (s[i] == 'I')
sum += sum >= ? - : ;
if (s[i] == 'V')
sum += ;
if (s[i] == 'X')
sum += sum >= ? - : ;
if (s[i] == 'L')
sum += ;
if (s[i] == 'C')
sum += sum >= ? - : ;
if (s[i] == 'D')
sum += ;
if (s[i] == 'M')
sum += ;
}
return sum;
}
};
【Leetcode】【Easy】Roman to Integer的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- C# 写 LeetCode easy #13 Roman to Integer
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and ...
- LeetCode 13. 罗马数字转整数(Roman to Integer)
13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符 数值 I 1 V ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- 【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
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- sharepoint_study_4
描述:如何向sharepoint自定义列表,新建(编辑)页面的网站栏后,添加一个快捷方式? 如图示: 解决: 1.在SharePoint Designer中打开该自定义列表 2.右键NewForm.a ...
- JAVA 使用Dom4j 解析XML
[转自] http://blog.csdn.net/yyywyr/article/details/38359049 解析XML的方式有很多,本文介绍使用dom4j解析xml. 1.环境准备 (1)下载 ...
- poj3176
一.题意:给定一些数,成三角形排列.从上往下走,每个数只能往它相邻的两个数走,一直走到底端得到一条线路.这条线路上的数的和最大是多少 二.思路:简单的动态规划.dp[i+1][j+1]:=以第i+1行 ...
- PIE SDK栅格数据的投影转换
1. 功能简介 为了适应不同数据显示分析的需要,数据的投影可以进行相应的转换,目前PIE SDK支持多种数据格式的投影转换,下面对栅格数据格式的投影转换功能进行介绍. 2. 功能实现说明 2.1. 实 ...
- rabbitmq 事务消息
事务消息主要用在发送方 在connection上加上事务属性, 发送方感知到本地事务执行失败, 需要通知broker将先前已经接收到的消息rollback,不要发给后面的消费者, 满足强一致性的要求 ...
- js 中的! 和 !! 的区别
Js中!的用法是比较灵活的,它除了做逻辑运算常常会用!做类型判断,可以用!与上对象来求得一个布尔值,1.!可将变量转换成boolean类型,null.undefined和空字符串取反都为false,其 ...
- Oracle 查询单挑语句运行时间
pl/sql 不考虑网络IO等影响 declare guidname ):='255fc3db-815a-46c1-9187-ec0256305335'; timespa timestamp:=CUR ...
- 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp
「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...
- jquery中Ajax提交配合PHP使用的注意事项-编码
问题:Ajax提交的数据的编码为utf-8,并且返回的数据也要求是utf-8的,如果说你的系统不是utf-8编码的话,那会让你痛不欲生! 解决方法:(比较笨拙的方法,但是很好用) 对于接收的数据,使用 ...
- avalon实现分页组件
前言 分页组件比较常见,但是用avalon实现的见的不多,这个分页组件,可以适配2种分页方式, 第一种是每次点击下一页,就请求一次后台,并返回当页数据和总条数,我称之为假分页: 第二种是一次性把所有数 ...