LeetCode 13. Roman to Integer(c语言版)
题意:
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 beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can 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. 题解:
还不太会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语言版)的更多相关文章
- 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, ...
- [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 ...
- 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 ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [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 ...
- [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 ...
- [leetcode] 13. Roman to Integer
如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...
随机推荐
- codeforces 792A-D
先刷前四题,剩下的有空补. 792A New Bus Route 题意:给出x 轴上的n 个点,问两个点之间的最短距离是多少,有多少个最短距离. 思路:排序后遍历. 代码: #include<s ...
- laravel 预加载特定的列
/**订单列表 0 已删除 1执行中 2 已过期 * * @param Request $request * * @return \Illuminate\Contracts\View\Factory| ...
- 五、Redis持久化配置
转载:[https://www.cnblogs.com/xingzc/p/5988080.html] Redis提供的持久化机制(RDB和AOF) Redis提供的持久化机制 Redis是一种面向“k ...
- 二、Redis安装
一.下载Redis: 下载地址:https://github.com/MSOpenTech/redis/releases. 由于redis并不支持window系统,而window版本的redis的是由 ...
- Java遍历List5种方法的效率对比
package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** ...
- Fiddle Proxy
1.抓包原理 Fiddler是类似代理服务器的形式工作,它能够记录所有你的电脑和互联网之间的http(S)通讯,可以查看.修改所有的“进出”的数据.使用代理地址:127.0.0.1, 默认端口:888 ...
- placeholder中文字添加换行方法
需求: 文本域内的提示文字两行显示 解决方案: 表示回车 表示换行 <textarea id="textarea" maxlength="22" plac ...
- 【dp】 背包问题
问题一:01背包 题目: [题目描述] 一个旅行者有一个最多能装 M 公斤的背包,现在有 n件物品,它们的重量分别是W1,W2,...,Wn它们的价值分别为C1,C2,...,Cn求旅行者能获得最大总 ...
- thinkphp 5.0 在appache下隐藏index.php入口代码
一.在appache的配置文件httpd.conf中开启rewrite_module 二.启用.htaccess的配置 启用.htaccess,需要修改httpd.conf,启用AllowOverri ...
- python验证卡普耶卡(D.R.Kaprekar)6174猜想
1955年,卡普耶卡(D.R.Kaprekar)对4位数字进行了研究,发现一个规律: 对任意各位数字不相同的4位数,使用各位数字能组成的最大数减去能组成的最小数,对得到的差重复这个操作,最终会得到61 ...