题目来源:

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. liunx 同步时间

    ntpdate stdtime.gov.hk (美国) time.nist.gov (复旦)(国内用户推荐) ntp.fudan.edu.cn 微软公司授时主机(美国) time.windows.co ...

  2. stl 迭代子的失效

    迭代子是STL中很重要的特性,但是其很脆弱(我个人认为),因为使用它的条件很苛刻,一不小心就失效了.其在两中情况下可能会失效. 1.当容器有插入操作时 在初始化了迭代子后,如果容器有插入操作时,迭代子 ...

  3. cocos2dx CCEditBox

    CCTextFieldTTF是一个简单的封装,用起来不是那么便利,在cocos2dx的extension里,对输入框有更加简单使用的类,那就是CCEditBox 上代码先: bool EditBox0 ...

  4. 具有 CSA CCM 证明的 SOC 2 可简化 Windows Azure 客户的安全性评估过程

    编辑人员注释:本文章由 Windows Azure 产品市场营销总监 Sarah Fender 撰写. 今天,我们宣布 Microsoft 的公共审计师 Deloitte 已经发布了有关 Window ...

  5. ollicle.com: Biggerlink – jQuery plugin

    ollicle.com: Biggerlink – jQuery plugin Biggerlink – jQuery plugin Purpose Demo Updated for jQuery 1 ...

  6. Java Native Interface Specification(JNI)

    Java Native Interface Specification(JNI) 使用场景: 需要的功能,标准的java不能提供 有了一个用其他的语言写好的工具包,希望用java去访问它 当需要高性能 ...

  7. 校园招聘 - 比較easy的面试题

    又到校园招聘的季节了, 自从和一些同事出版了<编程之美>一书之后, 我常常收到一些关于面试, 编程,  和"题库"的询问. 事实上我自己对算法没有什么研究, 有些问题都 ...

  8. java 反射提取类信息, 动态代理 和过滤某些方法演示

    java 反射提取类信息, 动态代理 和过滤某些方法演示 package org.rui.classts.reflects; import java.lang.reflect.Constructor; ...

  9. 编译ycm库

    在安装完YCM之后,重新打开vim还会出现如下的报错信息:ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; ...

  10. 【中英对照】【EntLib6】【Unity】实验1:使用一个Unity容器

    Lab 1: Using a Unity Container 实验1:使用一个Unity容器 Estimated time to complete this lab: 15 minutes 估计完成时 ...