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, ...
随机推荐
- Js中的判空
1.JS 中判断 undefined JavaScript 中有两个特殊数据类型:undefined 和 null var test= undefined; if (typeof(test) == u ...
- windows apache "The requested operation has failed" 启动失败
找到失败原因,进入cmd(win+r快捷键,输入cmd)命令行下 进入到你的apache bin目录下: 每个人错误可能不同,根据自己问题去相应改
- python django day 2
django-admin.py startproject zqxt_viewscd zqxt_viewspython manage.py startapp calczqxt_views/urls.py ...
- 浅谈log4j-5-读取properties文件(转自godtrue)
#### 在代码中配置log4j环境的方式,我们已经见识过了,是不是感觉比较麻烦,我们试试使用配置文件的方式是否使您的应用程序更加的灵活.# Log4j支持两种配置文件格式,一种是XML格式的文件,一 ...
- linux30道运维面试题
传送门https://zhangge.net/1986.html
- bash scripts收集
只保留代码中的头文件声明 #! /bin/sh echo "leave only INCluding declaration in c files" find $1 -name ...
- MySQL Transaction--两阶段提交事务
分布式事务两阶段提交 在分布式事务中,需要协调所有分布式原子事务参与者,并决定提交或回滚分布式事务,因此采用两阶段提交协议: 第一阶段为请求阶段或表决阶段,事务协调者通知事务参与者准备提交或取消事务, ...
- ORM 之常用重点 ******
总体介绍orm>>点我 单表查询api汇总 如 distinc(),order_by() id__gt=1 id__inrange=[ 1,3 ] 左右都包含 设置logg ...
- Abstract Data Types in C
Interface declares operations, not data structure Implementation is hidden from client (encapsulatio ...
- ThinkPHP3.2 where方法的使用
where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询.表达式查询.快捷查询.区间查询.组合查询在内的查询操作.where方法 ...