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 M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: "III"
Output: 3
Example 2:
Input: "IV"
Output: 4
Example 3:
Input: "IX"
Output: 9
Example 4:
Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 5:
Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 代码:
static void Main(string[] args)
{
string str = "LVIV";
int res=RomanToInteger(str);
Console.WriteLine(res);
Console.ReadKey();
} private static int RomanToInteger(string str)
{
int res = ;
Dictionary<char, int> dic=new Dictionary<char, int> { { 'I', }, { 'V', }, { 'X', }, { 'L', }, { 'C', }, { 'D', }, { 'M', } };
for (int i = ; i < str.Length; ++i)
{
int val = dic[str[i]];
if (i == str.Length - || dic[str[i + ]] <= dic[str[i]])
{
res += val;
}
else
{
res -= val;
}
}
return res;
}
解析:
输入:字符串
输出:整数
思想:
首先,分别将单个罗马数和其所对应的整数存入字典中。
其次,对于输入的罗马数,将其看作字符串。设置目前数为0,开始遍历,根据规律,从第一个字符到倒数第二个字符,每个字符在字典中的值与后一个字符比较,若前者大于后者,说明是类似于IV一样的,需要用目前的数减去这个值。否则,用目前的数加上这个值。若循环到最后一个字符,则其在字典中的值直接相加,直到循环结束。
最后,返回结果。
时间复杂度:O(n)
C# 写 LeetCode easy #13 Roman to Integer的更多相关文章
- 【LeetCode】13. Roman to Integer (2 solutions)
		
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
 - 《LeetBook》leetcode题解(13):Roman to Integer[E]
		
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
 - 【一天一道LeetCode】#13. Roman to Integer
		
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
 - LeetCode题解(13)--Roman to Integer
		
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...
 - 【leetcode】13. Roman to Integer
		
题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...
 - 【LeetCode】13. Roman to Integer 罗马数字转整数
		
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
 - 「Leetcode」13. Roman to Integer(Java)
		
分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...
 - Leetcode#13. Roman to Integer(罗马数字转整数)
		
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
 - Leetcode  13.    Roman to Integer(水)
		
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
 
随机推荐
- iOS 10 权限配置大全
			
<!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问 ...
 - 2017-2018-1 20179209《Linux内核原理与分析》第五周作业
			
一.实验:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 环境说明 实验环境为 Ubuntu16.10 和 实验楼环境. 选择39号系统调用实验.39号系统调用为mkdir系统调用. ...
 - Hadoop实战-Flume之自定义Source(十八)
			
import java.nio.charset.Charset; import java.util.HashMap; import java.util.Random; import org.apach ...
 - 我的Java开发学习之旅------>计算从1到N中1的出现次数的效率优化问题
			
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数.比如f(1)=1:f(13)=6,问一个最大的能满足f(n)=n中的n是什么? 例如:f(13)=6, 因为1, ...
 - Android Development Note-01
			
Eclipse快捷键: 导包:ctrl+alt+o 格式化代码:ctrl+alt+f MVC: M——Model V——View C——Control android程序界面如何设计.调试 U ...
 - JAVA各版本更新特性1-8
			
JAVA各版本更新特性1-8 原文地址 Java Versions, Features and History This article gives you a highlight of import ...
 - python优缺点小结
			
优点: 1.语言简洁优美 例如去除了大括号,写法简单,写法更接近于英语,其他语言几十上百行的代码,十来行就能解决,而且还好看 2.跨平台,window.linux.mac通用 3.排行高,社区完善 ...
 - 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5 ,PHP5.6)支持。
			
本人预装了一个wamp的集成环境(Apache 2.4.9 + PHP 5.5.12 + mysql 5.6.17),今天在wamp环境下 添加PHP多版本 (PHP 5.5.30).中间两个过程,1 ...
 - HDU 4652 Dice:期望dp(成环)【错位相减】
			
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4652 题意: 给你一个有m个面的骰子. 两种询问: (1)"0 m n": “最后 ...
 - css书写规则
			
无规矩不成方圆,不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的 不要在自闭合(self-closing)元素的尾部添加斜线 不要省略可选的结束标签(closing tag)(例 ...