题目链接

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. SpringCloud使用Feign出现java.lang.ClassNotFoundException: org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory异常

    废话不多说!!! 在SpringCloud项目中配置了Feign来调用restful接口,项目启动的时候报错,报错信息如下: 找不到org.springframework.cloud.client.l ...

  2. 【Python学习笔记】正则表达式

    Ref:https://deerchao.net/tutorials/regex/regex.htm#greedyandlazy 1. 常用元字符 2.字符转义 查找元字符本身时,需要使用\来取消这些 ...

  3. windows上的mysql配置过程

    个人电脑的mysql配置,记录下来留作备忘 1. 首先去官网下载最新的mysql安装包,我下的是5.7.25,地址是 https://dev.mysql.com/downloads/windows/ ...

  4. 第十一次PSP

  5. Notes of Daily Scrum Meeting(11.15)

    Notes of Daily Scrum Meeting(11.15) 今天周六我们的主要工作是把这周落下的一些工作补回来,这是写程序的最后阶段,准备进入测试阶段了,所以之前的工作 要补齐,今天大家的 ...

  6. HTML和CSS <h1> --2-- <h1>

    认识html文件基本结构 这一节中我们来学习html文件的结构:一个HTML文件是有自己固定的结构的. <html> <head>...</head> <bo ...

  7. iOS- Exception Type: 00000020:什么是看门狗机制

      1.前言    前几天我们项目闪退之后遇到的一个Crash,之后逛了许多论坛,博客都没有找到满意的回复  在自己做了深入的研究之后,对iOS的看门狗机制有了一个基本的了解  而有很多奇怪的Cras ...

  8. 用windbg检查.NET线程池设置

    比如我们在machine.config中进行了这样的设置(8核CPU): <processModel maxWorkerThreads="100" maxIoThreads= ...

  9. Docker的volume机制实现容器数据的持久性存储

    1:可以启动一个容器的时候,临时指定挂载的volume,这个volume会自动创建,无需在宿主机上事先创建 docker run -it -v /busybox busybox:latest 登入到b ...

  10. 操作系统 cmd mini OS

    #include <stdio.h>#include <stdlib.h>#include <string.h> void word(char *a){ if(st ...