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, ...
随机推荐
- C++学习(十一)(C语言部分)之 练习
/* 1.if 输入一个成绩 进行分级 输入一个数字 判断是否是水仙花数 比如 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 2.switch 提示 先对数字做处理 ...
- [codeforces Mail.Ru Cup 2018 Round 3][B Divide Candies ][思维+数学]
https://codeforces.com/contest/1056/problem/B 题意:输入n,m 求((a*a)+(b*b))%m==0的(a,b)种数(1<=a,b<= ...
- js的以及前端框架
js定义:浏览器的脚本语言,用简单的语言实现浏览器的操控 基础语法:基础的运算.函数.对象(原型链.对象构造.class) 所有的框架都是基于原生的js来进行的 js的事件:什么时候.什么情况下处理的 ...
- 收集的dubbo博客
1.http://shiyanjun.cn/archives/category/opensource/dubbo 2.https://blog.csdn.net/hellozpc/article/de ...
- Load Balancing OpenSSH SFTP with HAProxy
In my previous post I described how we setup a Ubuntu Server (12.0.4) as an OpenSSH SFTP server. In ...
- Unity3d插件开发与SDK对接实战 学习
c++: 注意x86/x64,vs2015. #include "stdafx.h" extern "C" { int Add(int a, int b) { ...
- oracle-闪回技术2
闪回版本查询,用到了附加日志 闪回事务查询 http://blog.csdn.net/laoshangxyc/article/details/12405459 这个博客的备份与恢复可以参考 ##### ...
- FastAdmin 数据库备份插件更新到 v1.0.4
FastAdmin 数据库备份插件更新到 v1.0.4 下载地址: https://www.fastadmin.net/store/database.html 更新如下: 修复了忽略列表无效的 Bug ...
- Executor与ExecutorService
ExecutorService 接口继承了 Executor 接口,是 Executor 的子接口. Executor 接口定义了 execute()方法用来接收一个Runnable接口的对象,而 E ...
- SmartGit 授权Non-Commerical
Window: %APPDATA%/syntevo/SmartGit/ 搜索settings.xml, 并且移除这个文件 重启