【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 ...
随机推荐
- HDU6470 ()矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:f[n] = f[n-1] + f[n-2]*2 + n^3; f[1] =1 ; f[2] = 2 ...
- 洛谷P2709 小B的询问
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...
- HDU - 2089 数位DP 初步
中文题目,不要62和4 从高位往低位DP,注意有界标志limit的传递 dp2记忆有界情况下的计数结果,据说用处不大 我所参考的入门文章就是半搜索(有界)半记忆(无界)的 进阶指南中提出dfs维度有多 ...
- maria(mysql)的主从复制
一.mariadb的基本操作 1.远程连接 mysql -uroot -p -h 127.0.0.1 mysql -uroot -p -h 192.168.226.128 2.赋予远程连接的权限 gr ...
- Mercurial stuck “waiting for lock”, tortoisehg pull版本卡住在等待 解决办法
最近使用hg的时候,拖取版本一直卡住不动.报错类似waiting for lock on working directory of xxxx held by ''. 原本以为是网络不好或者hg安装有问 ...
- PIE SDK聚类
1.算法功能简介 聚类处理时运用形态学算子将临近的类似分类区域聚类并合并. PIE SDK支持算法功能的执行,下面对聚类算法功能进行介绍. 2.算法功能实现说明 2.1. 实现步骤 第一步 算法参数设 ...
- python学习8-闭包、迭代器(转载)
一.第一类对象: 函数名是一个变量,可以当普通变量使用,但它又是一个特殊的变量,与括号配合可以执行函数. 函数名的运用 1.单独打印是一个内存地址 2.可以给其他变量赋值 3.可以作为容器类变量的元素 ...
- Bloom filter和Counting bloom filter
Bloom filter原理: https://en.wikipedia.org/wiki/Bloom_filter 推导过程结合博客: https://blog.csdn.net/jiaomeng/ ...
- query纠错方法
1. 第一种,在norvig介绍的方法中,详细的阐述了argmaxc P(c|w)的转换和求解办法. 这个概率不好直接算,但可以根据贝叶斯定理等价于argmaxc P(w|c)*P(c) / P(w) ...
- JavaScript设计模式(二) - 单例模式
什么是单例模式? 单例模式从字面上的理解是不困难的,js上就是指只有一个对象实例. 为什么需要单例模式? 我们可以将一些成员变量封装在一个单例对象中,每次访问这些变量都只能从这个单例对象进行访问,这样 ...