题意:

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:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (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. 题解:
  还不太会c++,所以现在就用c代替了。 代码:
int romanToInt(char* s) {
int len=strlen(s);
int sum=;
for(int i=;i<len;i++)
{
if(s[i]=='I')
{
if(s[i+]=='V')
{
sum+=;
i++;
}
else if(s[i+]=='X')
{
sum+=;
i++;
}
else
{
sum++;
}
continue;
}
if(s[i]=='V')
{
sum+=;
continue;
}
if(s[i]=='X')
{
if(s[i+]=='L')
{
sum+=;
i++;
}
else if(s[i+]=='C')
{
sum+=;
i++;
}
else
{
sum+=;
}
continue;
}
if(s[i]=='L')
{
sum+=;
continue;
}
if(s[i]=='C')
{
if(s[i+]=='D')
{
sum+=;
i++;
}
else if(s[i+]=='M')
{
sum+=;
i++;
}
else
{
sum+=;
}
continue;
}
if(s[i]=='D')
{
sum+=;
continue;
}
if(s[i]=='M')
{
sum+=;
continue;
}
}
return sum;
}

LeetCode 13. Roman to Integer(c语言版)的更多相关文章

  1. 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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  4. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

    1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  5. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  6. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  7. [leetcode]13. Roman to Integer罗马数字转整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  8. [LeetCode] 13. Roman to Integer ☆☆

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  9. [leetcode] 13. Roman to Integer

    如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...

随机推荐

  1. Python进阶8---面向对象基础1

    面向对象 语言的分类 Python的类 定义 class ClassName: pass class MyCalss: """A example class"& ...

  2. TOMCAT原理详解及请求过程(转载)

    转自https://www.cnblogs.com/hggen/p/6264475.html TOMCAT原理详解及请求过程 Tomcat: Tomcat是一个JSP/Servlet容器.其作为Ser ...

  3. 【CSS】利用宽高比例的媒体查询

    aspec-ratio 取值:value (x/y) 接收min/max前缀:是 aspect-ratio描述了输出设备目标显示区域的宽高比.该值包含两个以/分隔的正整数.代表了水平像素数(第一个值) ...

  4. Go语言中数组

    数组是一个值类型 func ArrayTest1(){ var arryA [3]int = [3]int{1,2,3} //创建一个数组B,将B中第二个元素设置为200 arryB := arryA ...

  5. 数据分析三剑客之pandas

    Pandas 引入 前面一篇文章我们介绍了numpy,但numpy的特长并不是在于数据处理,而是在它能非常方便地实现科学计算,所以我们日常对数据进行处理时用的numpy情况并不是很多,我们需要处理的数 ...

  6. 「POI2011 R2 Day2」Tree Rotations【线段树合并】

    题目链接 [BZOJ] [洛谷] [LOJ] 题解 由于是前序遍历,那么讨论一棵树上的逆序对的情况. 两个节点都在左子树上 两个节点都在右子树上 两个节点分别在不同的子树上. 前两种情况其实也可以归结 ...

  7. Windows系统CredSSP漏洞修复

    Windows系统凭证安全支持提供商协议 (CredSSP) 中存在一个严重漏洞,影响所有 Windows 版本,可导致远程攻击者利用 RDP 和 WinRAW 窃取数据并运行恶意代码. 详见:htt ...

  8. 大规模使用 Apache Kafka 的20个最佳实践

    必读 | 大规模使用 Apache Kafka 的20个最佳实践 配图来源:书籍<深入理解Kafka> Apache Kafka是一款流行的分布式数据流平台,它已经广泛地被诸如New Re ...

  9. Cucumber语法格式

    @login Feature: Login @T1 Scenario: Login with correct credentail Given I open login page When I ent ...

  10. JS学习笔记Day21

    一.mySQL数据库 (一)数据库的概念 1.概念:可以存储数据的一个仓库 2.结构化数据:以表格的形式展现,结构更清晰,这样的数据称之为结构化数据 (二)数据库管理软件 1.一种对数据库文件进行管理 ...