题目链接

Integer to Roman - LeetCode

注意点

  • 考虑输入为0的情况

解法

解法一:从大到小考虑1000,900,500,400,100,90,50,40,10,9,5,4,1这些数字,大于就减去,直到为0。时间复杂度为O(n)

class Solution {
public:
string intToRoman(int num) {
string ans = "";
while(num > 0)
{
if(num >= 1000)
{
ans += "M";
num -= 1000;
}
else if(num >= 900)
{
ans += "CM";
num -= 900;
}
else if(num >= 500)
{
ans += "D";
num -= 500;
}
else if(num >= 400)
{
ans += "CD";
num -= 400;
}
else if(num >= 100)
{
ans += "C";
num -= 100;
}
else if(num >= 90)
{
ans += "XC";
num -= 90;
}
else if(num >= 50)
{
ans += "L";
num -= 50;
}
else if(num >= 40)
{
ans += "XL";
num -= 40;
}
else if(num >= 10)
{
ans += "X";
num -= 10;
}
else if(num >= 9)
{
ans += "IX";
num -= 9;
}
else if(num >= 5)
{
ans += "V";
num -= 5;
}
else if(num >= 4)
{
ans += "IV";
num -= 4;
}
else if(num >= 1)
{
ans += "I";
num -= 1;
}
}
return ans;
}
};

小结

  • 终于有一次击败100%了!!不过这题难度为什么会是中等啊...

Integer to Roman - LeetCode的更多相关文章

  1. Integer to Roman -- LeetCode 012

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

  2. Integer To Roman leetcode java

    问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  3. integer to roman leetcode c++实现

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

  4. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

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

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

  6. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  7. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  8. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  9. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

随机推荐

  1. bootstrap switch样式修改与多列等间距布局

    先以一张图开启今天的随笔 今天实习遇到了switch按钮,小姐姐说用插件bootstrap switch来写,我第一次用这个插件,首先在引入方面就遇到了很多坑,先来总结一下bootstrap swit ...

  2. 我看微软收购GitHub

    今天是微软收购GitHub的第三天,之前很多人担心被微软收购的GitHub会步Skype,诺基亚等企业的后尘,凡此种种我觉得更多人的担心是:GitHub不再开源免费罢了. GitHub今年4月刚成立十 ...

  3. PLSQL函数,存储过程

    --创建一个函数,用来根据部门编号返回调薪幅度 create or replace function get_ratio_by_dept(deptno varchar2) return number ...

  4. Discuz3.3精仿小米风格整站模板制作——1、新建模板方案

    术语说明: 模板——模板是一堆按照规定命名方式的html文件,用于指定整个论坛不同页面的外观. 标签——标签和模板共同作用以实现论坛换肤功能,其中标签主要控制页面显示什么数据,显示多少条等. 风格—— ...

  5. HTML学习1-Dom之事件绑定

    事件: 1.注册事件 a. <div onxxxx=””></div> b. document  .onxxxx= function()  //找到这个标签 2.this,触发 ...

  6. Sentence | Never underestimate yourself.

    "\(Our\) \(deepest\) \(fear\) \(is\) \(not\) \(that\) \(we\) \(are\) $inadequate. $ \(Our\) \(d ...

  7. 笔试题——C++开发简单记录错误模块

    题目:链接:https://www.nowcoder.com/questionTerminal/67df1d7889cf4c529576383c2e647c48 来源:牛客网 解析及代码来源:http ...

  8. Hadoop错误码速查

    经常遇到的exception是:PipeMapRed.waitOutputThreads(): subprocess failed with code N "OS error code 1: ...

  9. tensorflow enqueue_many传入多个值的列表传入异常问题————Shape () must have rank at least 1

    tf 的队列操作enqueue_many传入的值是列表,但是放入[]列表抛异常 File "C:\Users\lihongjie\AppData\Local\Programs\Python\ ...

  10. spring-session实现分布式集群session的共享(转)

    原文: https://www.cnblogs.com/youzhibing/p/7348337.html HttpSession是通过Servlet容器创建和管理的,像Tomcat/Jetty都是保 ...