leetCode练题——13. Roman to Integer
1、题目
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.
2、我的解法
罗马数字转化成阿拉伯数字,多重判断。。。。
# -*- coding: utf-8 -*-
# @Time : 2020/1/28 14:35
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 13. Roman to Integer(字符串索引).py class Solution:
def romanToInt(self, s: str) -> int:
l = len(s)
y = 0
for i in range(l):
if s[i]=="V"and s[i-1]=="I"and i!=0:
y=y+3
elif s[i]=="X"and s[i-1]=="I"and i!=0:
y=y+8
elif s[i]=="L"and s[i-1]=="X"and i!=0:
y=y+30
elif s[i]=="C"and s[i-1]=="X"and i!=0:
y=y+80
elif s[i]=="D"and s[i-1]=="C"and i!=0:
y=y+300
elif s[i]=="M"and s[i-1]=="C"and i!=0:
y=y+800
elif s[i] == "I":
y = y + 1
elif s[i] == "V":
y = y + 5
elif s[i] == "X":
y = y + 10
elif s[i]== "L":
y = y + 50
elif s[i] == "C":
y = y + 100
elif s[i] == "D":
y = y + 500
elif s[i] == "M":
y = y + 1000
if y>=1 and y<=3999:
return y
else:
return 0 print (Solution().romanToInt("MMMCDXC"))
leetCode练题——13. Roman to Integer的更多相关文章
- [LeetCode&Python] Problem 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
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
- 【leetcode❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- 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, ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【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 ...
随机推荐
- C 语言实现面向对象编程
转载 https://blog.csdn.net/onlyshi/article/details/81672279 C 语言实现面向对象编程1.引言面向对象编程(OOP)并不是一种特定的语言或者工具, ...
- 1.1Jmeter学习网站
在网上找到一些关于Jmeter学习的博客等,在此标记 一. chinaunix的一篇博客,讲的蛮详细的 http://blog.chinaunix.net/uid/26884465/cid-16819 ...
- 转载:进程退出状态--waitpid status意义
最近遇到一个进程突然退出的问题,由于没有注册signalhandler所以没有捕捉到任何信号. 但是从log中看到init waitpid返回的status为0x008b,以前对status不是很了解 ...
- 一个c程序的执行是从什么开始的?
一个C语言的执行是从本程序的main函数开始,到main函数结束,但需要注意的是,也是有特殊情况的,若是代码中出现了exit函数,则直接结束程序,对于这一点还是需要了解的. 关于c语言需要了解的是,C ...
- T114048 [RC-02] yltx数对 (打表)
这题如果全部打表的话,文件大小会有65kb,超了,所以只打出一半,前一半用程序算就可以了,并不会超时. 如果算法优化的好,其实可以打的更少. #include <bits/stdc++.h> ...
- [Python] Tkinter的食用方法_01_简单界面
#开始 放假之后感觉整个人已经放飞自我了,完全不知道自己一天天在干什么,明明有很多的事情需要做,但是实际上每天啥都没做,,,虚度光阴... 晚上突然心烦意乱,开始思考今天一天都做了什么,感觉很有负罪感 ...
- 炸鱼w咕咕咕
退役选手又回来了w + 将近一个月莫有学OI变得十分笨拙 + 洛谷打卡从300+ 变为 10 + 冬令营加油(虽然我会崩 + 省选加油 (虽然我进不去
- mybatis实现有则更新无则插入
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id" ke ...
- 【嵌入式】ARM9复习
一.嵌入式系统基础 二.ARM处理器 1. 在每条指令后,用;//注释这条指令的寻址方式,以及实现的功能(25分) 注:变址寻址需要标注是基址加偏移.还是基址加索引,是前变址还是后变址.SUB SP, ...
- Win Oracle 监听文件配置参考
Win lister.ora配置参考 # listener.ora Network Configuration File: C:\app\Administrator\product\\dbhome_1 ...