【LeetCode】12. Integer to Roman 整型数转罗马数
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路:
主要是了解罗马数和阿拉伯数字的对应关系,如下表:

由这个表基本上可以将1-3999范围的阿拉伯数字换成罗马数字。在处理阿拉伯数字时从高位开始匹配,将每个位的值找出对应罗马数字,串成字符串即可。
public class Solution {
public String intToRoman(int num) {
int[] val={1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] sym={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
String rom="";
for(int i=0;i<val.length;i++){
while(num>=val[i]){
rom+=sym[i];
num-=val[i];
}
}
return rom;
}
}
【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/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
随机推荐
- Puppet's Commands 3.7
Puppet's Commands Puppet’s command line interface consists of a single puppet command with many subc ...
- 战胜忧虑<3>——学会接受不可避免的事实。
学会接受不可避免的事实. 对必然的事情愉快地承受,就像杨柳承受风雨,水接受一切容器,我们也要承受一切事实. 故事: 在美国庆祝陆军在北非获胜的那一天,我接到国防部送来的一封电报,我的侄儿——我最爱的一 ...
- 【WEB】原理 之 线程池
问题描述:我们获取连接超过连接池最大值时产生如上异常.通常连接池最大值为100.当我们获取连接超过最大值时,WEB等待连接池返回连接而超时,这样将抛出如上异常解决办法:首先要做的是在我们使用连接后立即 ...
- Css3_写出小广播样子
/* create an arrow that points up */ div.arrow-up { width:0px; height:0px; border-left:5px solid tra ...
- 如何实现一个malloc
任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉.但是,许多程序员对malloc背后的事情并不熟悉,许多人甚至 ...
- HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...
- 错误记录 git pull
在安装open-falcon的nodata组件,更新库的时候,git pull 报错: You are not currently on a branch, so I cannot use any'b ...
- git push失败的解决办法(2)
错误一:Cannot rebase: You have unstaged changes 解决办法: Cannot rebase: You have unstaged changes. 那说明有修改过 ...
- listview--记录ListView滚动停止位置与设置显示位置
在项目中经常使用到listView控件,当想记录滚动停止时的记录,当点击加载新的数据,从记录的位置开始显示的操作怎么实现尼?分为如下步骤 1.记录位置代码 //声明记录停止滚动时候,可见的位置 pri ...
- ylbtech-Unitity-cs:传递的字符串中数字字符的数目
ylbtech-Unitity-cs:传递的字符串中数字字符的数目 1.A,效果图返回顶部 1.B,源代码返回顶部 1.B.1, using System; namespace Functions ...