LeetCode——12. Integer to Roman
一.题目链接:https://leetcode.com/problems/integer-to-roman/
二.题目大意:
给定一个整数,返回它的罗马数字的形式。
三.题解:
要想做出这道题目,首先应该弄清楚罗马数字的规律。罗马数字中的任意一个字符连写不会重复出现4次,最多连续出现3次。题目给定的数字范围是1~3999,所以说不用特意去考虑这一点了,按照平常的思路去做就行了。给定一个罗马数字,由于它最多为4位,所以只需拆成个分位、十分位、百分位和千分位即可。对于每个位置的数字对应哪个罗马数字,只要对应起来最后拼成一起即可。代码如下:
class Solution {
public:
string intToRoman(int num) {
char* roman[4][10] = {{"","I","II","III","IV","V","VI","VII","VIII","IX"},//个分位的罗马字母
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},//百分位的罗马字母
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},//十分位的罗马字母
{"","M","MM","MMM"}};//个分位的罗马字母
string romanNum;
romanNum.append(roman[3][num / 1000 % 10]);//千分位
romanNum.append(roman[2][num / 100 % 10]);//百分位
romanNum.append(roman[1][num / 10 % 10]);//十分位
romanNum.append(roman[0][num % 10]);//个分位
return romanNum;
}
};
本体的关键之处在于罗马数字的各个分位的数字如何表示。
LeetCode——12. Integer to Roman的更多相关文章
- 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 ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 12. Integer to Roman ☆☆
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Java [leetcode 12] Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- [leetcode]12. Integer to Roman整数转罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- [leetcode] 12. Integer to Roman
关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...
随机推荐
- Unity 3D观察者设计模式-C#委托和事件的运用
C#观察者设计模式 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新! ...
- Guarding the Chessboard(UVa 11214)
本题题意: 输入一个n*m的棋盘,某些格子有标记,用最少的皇后占据或者攻击所以带标记的格子.皇后的攻击范围为同行同列和同对角线. 可以使用IDA*算法,即从样例可以发现只需要最多5个棋子就可以对棋盘上 ...
- UV纹理+修改器:VertexWeightEdit+修改器:Mask遮罩
UV纹理+修改器: VertexWeightEdit+修改器: Mask遮罩 基本流程, 如下图,准备地图一份, 黑白色即可. 纹理使用颜色绘制权重. 白色为1, 黑色为0. 新增球体, 细分多次, ...
- acm 2044
////////////////////////////////////////////////////////////////////////////////#include<iostream ...
- PS学习之小猪佩奇身上纹,掌声送给社会人
首先准备素材 用ps首先打开素材一 首先对图片去色 快捷键:shift+Ctrl+u 调整色阶 设置高斯模糊: 另存为psd格式,命名为叠加的对象 再次打开素材一,把佩奇拖入到图层里,并调整大小,旋转 ...
- hdu4292 Food 最大流
You, a part-time dining service worker in your college’s dining hall, are now confused with a new pr ...
- python Console menu
I just finished a demo which is to provide an easy way to control hardware resources of A sample. Th ...
- jQuery -> 基于当前元素的遍历
版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/FeeLang/article/details/26257549 假设我们已经通过jQuery方法选中了一组元 ...
- C# to IL 7 Pointers(指针)
Pointers are the heart and soul of a programming language. The only reason why the Cprogramming lang ...
- adb command