【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[] = {"", ...
随机推荐
- android studio使用发布者证书调试
某些时候还是要用到的,直接说步骤,修改app.gradle apply plugin: 'com.android.application' android { .................... ...
- "aa".equals(name)这种写法为什么就可以避免空指针
public static void main(String[] args) { String name=null; if("aa".equals(name))//这种情形,不出现 ...
- c#处理3种json数据的实例
网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋 ...
- Hierarchy Viewer
http://blog.csdn.net/ddna/article/details/5527072 Hierarchy Viewer是随AndroidSDK发布的工具,位置在tools文件夹下,名为h ...
- MVC 安装
mvc 4 支持window xp,window 7,window 8, mvc 4 支持vs2010,vs2012 vs2012中包含mvc4; vs2010中需要安装mvc4 安装包:
- C#学习笔记三: C#2.0泛型 可控类型 匿名方法和迭代器
前言 C#1.0的委托特性使方法作为其他方法的参数来传递,而C#2.0 中提出的泛型特性则使类型可以被参数化,从而不必再为不同的类型提供特殊版本的实现方法.另外C#2.0还提出了可空类型,匿名方法和迭 ...
- shell 的语法
SHELL 的语法 n 变量:字符串,数字,环境和参数 n 条件:shell中的布尔值 n 程序控制:if, elif, for, while until, case n 命令列表 n 函数 ...
- NeHe OpenGL教程 第二十四课:扩展
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- php 防止sql注入
Q:如果把用户输入的没有任何改动的放到SQL的查询语句中,很有可能会导致SQL注入,比如说下面的例子: $unsafe_variable = $_POST['user_input']; mysql_q ...
- ruby的optparse使用小记
#自定义转换器 1 opts.accept(Hash) do |string| hash = {} string.split(',').each do |pair| key,value = pair. ...