【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[] = {"", ...
随机推荐
- Nmap命令的29个实用范例
Nmap即网络映射器对Linux系统/网络管理员来说是一个开源且非常通用的工具.Nmap用于在远程机器上探测网络,执行安全扫描,网络审计和搜寻开放端口.它会扫描远程在线主机,该主机的操作系统,包过滤器 ...
- VT100字体
自从接触LINUX之后,VT100是我最喜欢的终端字体,当然它也是SecureCRT的默认字体.真实文件全名,VT100.FON 总共才44KB大小. 字体安装:直接放入C:\Windows\Fon ...
- Tomcat DEBUG模式下修改代码立刻生效!
- 监控页面所有 ajax请求
监控所有ajax请求: 你是不是有遇到这样的问题:页面发起两个ajax请求,希望它们都成功以后,再做一个动作? 很容易想到的解决方案是,等其中一个结束以后,再发起另外一个,这个过程用回调函数来完成. ...
- js让iframe高度自动
HTML: <iframe id="yb_if" width="940px" src="连接" frameborder=0 allow ...
- js swipe 图片滑动控件实现 任意尺寸适用任意屏幕
http://www.swiper.com.cn/http://www.idangero.us/swiper/demos/ 解决问题点: 1.先得到图片真实的宽高, 根据真实宽高 等比例 2.调用的控 ...
- Apache Thrift学习之一(入门及Java实例演示)
目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...
- Java多线程之新类库中的构件PriorityBlockingQueue
package concurrent2; import java.util.ArrayList; import java.util.List; import java.util.Queue; impo ...
- bootstrap小例子等
一个简单的表单样式: <div class="row"> <form action="#" class="form-horizont ...
- 性能测试工具Gatling - 设置Recorder
Gatling自带的Recorder,可以大大节省我们书写scenario的时间. 用法和selenium的IDE类似,作为一个代理服务器在browser和application之间做桥梁作用 ...