题目来源:

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. clistctrl 虚拟列表

    一.什么是虚拟列表控件 虚拟列表控件是指带有LVS_OWNERDATA风格的列表控件.. 二.为什么使用虚拟列表控件 我们知道,通常使用列表控件CListCtrl,需要调用InsertItem把要显示 ...

  2. Sublime 插件安装、常用配置

    安装:sublime + 插件 安装Sublime: 官网:http://www.sublimetext.com/ 安装package control组件,之后我们会使用该组件给Sublime安装常用 ...

  3. passwordauthentication yes

    ssh ip disconnected:no supported authentication methods available(server sent:publickey,gssapi-keyex ...

  4. Apache Maven-AntRun-Plugin 官网 bug

    Maven AntRun Plugin 今天在 Apache maven-antrun-plugin 官网查找资料时,竟然发现了一个bug!! 在此记录下,以免在同一个地方摔倒两次! 想起一句话,尽信 ...

  5. request.getParameterValues与request.getParameter的差别

    一. 简单的对照 request.getParameter用的比較多,相对熟悉 request.getParameterValues(String   name)是获得如checkbox类(名字同样, ...

  6. iOS 中 Touch ID得使用方法

    iPhone 5S公布以后,iOS设备基本都有集成Touch ID.而这个功能对自己的App也是一个非常好的扩展,关于Touch ID的使用方法.大致例如以下, Swift: 引入LocalAuthe ...

  7. hadoop默认3个核心配置文件说明

    1       获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配置文件,默认下来,这些配置文件都是空的,所以很难知 ...

  8. ##DAY1 UI、frame、center、bounds、UIVIew

    ##DAY1 UI.frame.center.bounds.UIVIew #pragma mark ———————UI——————————— UI的本意是用户界面,是英文User和 Interface ...

  9. Myeclipse利用maven构建sping web项目

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQQAAADvCAIAAACbnj2oAAAfq0lEQVR4nO2d+1MUV9rH+294U9mqpG ...

  10. LintCode-乱序字符串

    题目描述: 给出一个字符串数组S,找到其中所有的乱序字符串(Anagram).如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中. 注意事项 所有的字符串都只包含小写字 ...