题目来源:

https://leetcode.com/problems/integer-to-roman/


题意分析:

这道题是要把在区间[1-3999]的数字转化成罗马数字。


题目思路:

只要知道了罗马数字和阿拉伯数字是怎么转换的就不难了,要注意的是900,500,400,90,50,40,9,5,4分别应该是‘CM’,‘D’,‘CD’,‘XC’,‘L’,‘XL’,‘IX’,‘V’,‘IV’。


代码(python):

 class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
a = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
b = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
ans = ''
i = 0
count = 0
while num > 0:
count = num/a[i]
num %= a[i]
while count > 0:
ans += b[i]
count -= 1
i += 1
return ans

转载请注明出处:http://www.cnblogs.com/chruny/p/4817819.html

[LeetCode]题解(python):012-Integer to Roman的更多相关文章

  1. leetCode练题——12. Integer to Roman

    1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C,  ...

  2. No.012 Integer to Roman

    12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...

  3. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

    12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...

  4. LeetCode--No.012 Integer to Roman

    12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...

  5. 【LeetCode】012. Integer to Roman

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

  6. 【JAVA、C++】LeetCode 012 Integer to Roman

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

  7. [Leetcode]012. Integer to Roman

    public class Solution { public String intToRoman(int num) { String M[] = {"", "M" ...

  8. leetcode解决问题的方法||Integer to Roman问题

    problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  9. leetcode第12题--Integer to Roman

    Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  10. 012 Integer to Roman 整数转换成罗马数字

    给定一个整数,将其转为罗马数字.输入保证在 1 到 3999 之间. 详见:https://leetcode.com/problems/integer-to-roman/description/ cl ...

随机推荐

  1. 如何使用沉浸式状态栏,让你的app风格更好看

    大家都知道,传统的手机状态栏非黑即白,经常让整个app显得不是那么的好看,如何让状态栏的颜色跟你整个界面的颜色能够融为一体,这是我们一直想要的,现在给大家展示一下: 由图可见,第一张是没有使用沉浸式状 ...

  2. CountDownLatch和CyclicBarrier区别及用法的demo

    javadoc里面的描述是这样的. CountDownLatch: A synchronization aid that allows one or more threads to wait unti ...

  3. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  4. ZOJ Monthly, March 2013

    A题 题目大意:给出一棵树,一开始节点值均为0,先要求完成在线操作:将某子树所有节点值取反,或者查询某子树总点权. 题解:很基础的线段树题,既然两个操作都是子树操作,那么就先树链剖分一下,将子树操作转 ...

  5. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

  6. 沙朗javascript(两)正则表达式

          以下提到的文章javascript基础,今天我说些什么javascript正则表达式正确.      说到这首先要问了,什么是正則表達式,正則表達式能干什么? 正則表達式:一个正則表達式就 ...

  7. Shell 脚本小试牛刀(番外) -- 捷报

    捷报 捷报 捷报 捷报 捷报 捷报来袭,本系列的脚本已在Github 上开了版块, 我命名为" easy shell "(点此进入). 眼下已加入前面几期中的脚本,日后还会有很多其 ...

  8. Windows 配置JAVA的环境变量

    Java是由Sun公司开发的一种应用于分布式网络环境的程序设计语言,Java语言拥有跨平台的特性,它编译的程序能够运行在多种操作系统平台上,可以实现“一次编写,到处运行”的强大功能. 工具/原料 JD ...

  9. gcc 的编译过程

    通常我们都是使用下面的命令来直接生成可执行文件 gcc demo.c -o demo 对于我们来说十分简单,但是对编译器来说却完成了一系列复杂的工作,概括起来有如下几步: 1. 预处理 gcc -E ...

  10. IOS 学习笔记(2) 视图UINavigationController

    1.栈 导航控制器自身有一个针对显示内容的栈,也有一个对于导航栏的栈,当有新的内容欲显示时,进的导航栏和显示内容会被压入此栈,这样原本显示中的导航栏和显示内容则会进入到栈的更深一层中,根据栈的先进后出 ...