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的更多相关文章
- 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 ...
- Integer To Roman leetcode java
问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- 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 ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【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 ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【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 ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
随机推荐
- Java子类初始化调用父类无参构造
实在是服了自己,子类初始化调用父类无参构造这种初学者都应该知道的事,我给忘了. 记得当初看书的时候各种概念抄在笔记本上,再上机实践,以为一辈子都不会忘,还是给忘了. 这件事说明了两个问题: 1.我没有 ...
- Array.Copy 数据是克隆吗?
偶然看到 Array.Copy 方法的时候,想到,它是否是克隆,又是否是深克隆. 做了一个测试 public class abc { public string hello; } [TestMetho ...
- 第三次作业---excel导入数据库及显示(2)
发现第一次做的功能有点复杂,不能理解.而且第一次的想法是在页面上上传文件,连接并导入到数据库,并在页面上显示.后来才看到要求是直接在本地将数据导入数据库就行了,然后显示.所以才出现了一堆看不懂也解决不 ...
- 团队GIT实战总结
项目要求 组长博客 遇到的困难及解决办法 组员1(组长):王彬 遇到的困难 在团队任务分工的时候没有充分照顾到所有人,导致队员们的工作量不均. 现场编程时间不够 解决办法 在此对组员们表示抱歉,由于 ...
- Week2:阅读笔记与思考
<构建之法>这本书的内容通俗易懂,每一个知识点都有许多事例佐证,阅读起来不像其他教科书那样枯燥无聊.但阅读过第一.二.十六章之后还是产生了几个疑问,以及更深层次的思考. 第一章 问题1: ...
- TensorFlow问题“Attempting to use uninitialized value”
1.出现的问题: 对已经保存好的模型,在进行重载并继续训练的过程中出现了以下问题: 2.解决办法: 在查找了相关资料后,了解到,该错误是指在从tfrecord中读取数据时一些参数未被初始化,如果直接r ...
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (二) 发送自定义数据
在我的项目里,树莓派主要作为中心节点,用于接收数据,Arduino作为子节点,用于发送数据,考虑到以后会有很多子节点,但又不至于使得代码过于繁琐,因此所有的传输数据添加一个头部编号用于区分不同节点. ...
- Centos7 虚拟机复制后网卡问题 Job for network.service failed
在运行“/etc/init.d/network restart”命令时,出现错误“Job for network.service failed. See 'systemctl status netwo ...
- 消息队列第一篇:MessageQueue介绍
消息队列有哪些好处或功能: 1.消息可以在断开连接的环境下发送.不需要同时运行正在发送和正在接收的应用程序. 2.使用快捷模式,消息可以非常快地发送.在快捷模式下,消息存储在内存中. 3.对于可恢复的 ...
- jQuery扩展插件
jQuery有多好用,大家有目共睹的,但是有时候不是每个功能都是万能的,有时候我们需要实现自己的功能,jQuery提供了很好的拓展功能,我们可以去拓展插件,更好的利用jQuery 查看官网,可知,有两 ...