LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
给定一个整数,将他转换到罗马字符。同样这里的罗马字符不会超过1000(M),PS:关于罗马字符的介绍看我的这篇文章 :LeetCode OJ:Roman to Integer(转换罗马字符到整数)
这里的大体思路是这样的,例如对于罗马字符952来说,起答题的可以分成三个组成部分, 就是 (1000 - 100):CM, (50): L, (1 + 1): II 这三个部分, 再看看1996, 实际上由1000 : M, (1000 - 100): CM, (100 - 10): XC, 5 : V, 1: I
也就是说900 400 90 40 9 4分别需要用两个的字母来表示,这些表示方法都是有限的,所以可以用一个vector意义列举出来,具体程序如下,比较难想到,一开始我也没想到, 看了 这篇文章 才懂了。代码如下:
class Solution {
public:
string intToRoman(int num) {
vector<int> digitNumber{, , , , , , , , , , , , };
vector<string> alpha{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string result = "";
int sz = digitNumber.size();
for(int i = ; num != 0; ++i){
while(num >= digitNumber[i]){
num -= digitNumber[i];
result.append(alpha[i]);
}
}
return result;
}
};
LeetCode OJ:Integer to Roman(转换整数到罗马字符)的更多相关文章
- leetcode:Integer to Roman(整数转化为罗马数字)
Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- Leetcode(8)字符串转换整数
Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- 【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题解——Integer to Roman
题目: 将整数转换为罗马数字.罗马数字规则可以参考: 维基百科-罗马数字 解法: 类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换. 代码: class Solution { publ ...
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
随机推荐
- myeclipse如何删除自带Javaee里面jar包
myeclipse是我们在使用Java开发时的一款不错的集成开发环境软件,一般在开发web项目的时候,都要引入相关的jar包,javaee包就是其中一个,有时候其里面的jar包可能与我们需要的不匹配, ...
- CRM——插件流程回顾
1. Django项目启动 自动加载文件 制作启动文件 1. 注册strak 在apps.py 类里面增加如下 def ready(self): from django.utils.module_lo ...
- python 2 和python 3的 区别
用户交互 input ps:python2:raw_input python3:input 在 python2里 print不需要加括号也可以打印 子python3里 print 必须加括号才能打印
- 把RedisWatcher安装为windows服务
安装完成后, 到安装目录下修改watcher.conf.注意,任何路径都不可包含空格,中文,特殊字符,且全部使用绝对路径配置文件中文注释exepath --> redis-server.exe的 ...
- 20包含min函数的栈
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 建一个辅助栈,把每次最小最小的元素(之前最小元素,与当前新入栈的元素比较)放在辅助栈里. import j ...
- PAT 天梯赛 L1-012. 计算指数 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-012 AC代码 #include <iostream> #include <cstdio&g ...
- Spring @Qualifier l转
当候选 Bean 数目不为 1 时的应对方法 在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...
- java鲁棒性(健壮性)
java能检测编译和运行时的错误 java自己操作内存减少了内存出错的可能 java实现了真数组,避免了覆盖数据的可能 Java不支持指针操作,大大减少了错误发生的可能性 ... 备注: Java能运 ...
- CentOS 7如何将.deb文件转换.rpm
1.首先下载alien工具 http://ftp.de.debian.org/debian/pool/main/a/alien/ http://ftp.de.debian.org/debian/po ...
- Kali更新源,亲测目前可用的源
kali更新的时候老是无法定位软件包,网络上大部分中科大.阿里云kali源都不可用,都千篇一律,最后找了这个,网易的,还不错,贴出来大家看看: # 源 deb http://mirrors.163.c ...