题目链接

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. 角色和武器Shader特效开发

    角色Shader的动效需求 角Shader的开发不知要实现最基础光照等功能, 可能还要在角色武器的Shader增加多种动效, 比如因武器品质区分的流光特效, 被技能击中时的冻结效果. 这类动效的实现方 ...

  2. 安装VMware-tools时,一直停在“The path "" is not valid path to the gcc binary.”

    解决方案: 1.先停止安装(ctrl+Z) 2.在终端输入: yum -y update yum -y install kernel-headers kernel-devel gcc 3.重新安装VM ...

  3. OpenFastPath(1):快平面接口是否支持多ip

    1.配置环境 fp0接口上配置两个IP地址: fp0       Link encap:Ethernet  HWaddr 00:0c:29:30:38:db inet addr:192.168.56. ...

  4. 在IIS中部署Asp.Net网站

    在IIS中部署Asp.Net网站 1.添加IIS或者删除IIS,在控制面板=>程序和功能=>打开或关闭功能 启动iis,右键计算机=>管理=>服务和应用程序=>Inter ...

  5. 对PBFT算法的理解

    PBFT论文断断续续读了几遍,每次读或多或少都会有新的理解,结合最近的项目代码,对于共识的原理有了更清晰的认识.虽然之前写过一篇整理PBFT论文的博客,但是当时只是知道了怎么做,却不理解为什么.现在整 ...

  6. Python常用模块之PIL(手册篇:Image模块)

    官方手册地址:http://effbot.org/imagingbook/image.htm  Image模块 图像模块提供了一个具有相同名称的类,用于表示一个PIL的图像.该模块还提供了许多功能,包 ...

  7. Python数据分析工具库-Numpy 数组支持库(一)

    1 Numpy数组 在Python中有类似数组功能的数据结构,比如list,但在数据量大时,list的运行速度便不尽如意,Numpy(Numerical Python)提供了真正的数组功能,以及对数据 ...

  8. Django_cookie+session

    一.cookie和session介绍 cookie 由服务器产生内容,浏览器收到请求后保存在本地:当浏览器再次访问时,浏览器会自动带上cookie,这样服务器就能通过cookie的内容来判断这个是“谁 ...

  9. Beta阶段基于NABCD评论作品

    组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶  刘佳瑞  公冶令鑫  杨磊  刘欣  张宇  卢帝同 一.拉格朗日2018--<飞词> 1.1.NABCD分析 N(Need,需求):该小 ...

  10. YQCB冲刺周第七天

    站立会议 任务看板 燃尽图 今天的任务为实现个人设置中的修改密码.设置金额的功能.以及界面的美化. 遇到的问题为修改自己密码时获得当前用户的id问题.