题意:

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. 洛谷P2756 飞行员配对方案问题

    二分图裸题,找他的最大匹配即可 #include<bits/stdc++.h> using namespace std; int n,m,ans; ; int to[N]; struct ...

  2. iOS XIB使用中适配iPhoneX的安全区域、调用UiView动画

    2.调用UiView动画 WeakSelf; self.detailsViewBom.constant += 230; [UIView animateWithDuration:animotiontim ...

  3. 特殊计数序列——Catalan数

    Catalan数 前10项 \(1,1,2,5,14,42,132,429,1430,4862\) (注:从第\(0\)项起) 计算式 \(C_n=\frac{1}{n+1}\dbinom{2n}{n ...

  4. JavaScript千分符---正则实现

    一般在JavaScript中实现千分符,是使用切割+连接一顿操作 这里尝试一下使用正则快速实现千分符 let num0 = '12' let num1 = '123' let num2 = '1234 ...

  5. Sublime Text3—设置快捷键打开浏览器

    在不同浏览器查看代码效果可谓是家常便饭,所以用不同快捷键对应打开不同浏览器可以大大提高工作效率. 本篇分享个简单的方法只需二步: 一.安装插件SideBarEnhancements ctrl+shif ...

  6. 码云报错:fatal: remote origin already exists.解决方法

    今天在提交Git的时候,遇到了几个问题,记录一下,方便以后查找O(∩_∩)O 第一个问题 git remote add origin************** fatal: remote origi ...

  7. JMeter的介绍和简单使用

    Apache官网(https://jmeter.apache.org/)对JMeter的解释: Apache JMeter™ Apache JMeter™应用程序是开源软件,   为负载功能和性能测试 ...

  8. ES6随手学

    1.遍历字符串 for (let codePoint of 'foo') { console.log(codePoint) } 格式:for(let  print  of  string){  } p ...

  9. [Reinforcement Learning] Model-Free Prediction

    上篇文章介绍了 Model-based 的通用方法--动态规划,本文内容介绍 Model-Free 情况下 Prediction 问题,即 "Estimate the value funct ...

  10. Mac平台Clion配置GLFW+GLAD的项目

    前期的准备工作详见LearnOpenGL CN 看这篇教程的前提是假设你已经编译好了GLFW文件夹以及下载好了GLAD,不会的话可以看我的另一篇 文章的前部分: 配置 Clion新建一个项目,CMak ...