题目:

  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 整型数转罗马数的更多相关文章

  1. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  2. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. Java [leetcode 12] Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  7. [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 ...

  8. LeetCode——12. Integer to Roman

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

  9. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

随机推荐

  1. 【转】JDBC为什么要使用PreparedStatement而不是Statement

    http://www.importnew.com/5006.html PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedSta ...

  2. FTP主/被动模式的原理

    ---------------------------------------------------------------------------------------------------- ...

  3. android学习笔记18——dpi、dp、sp、xp......

    参考:http://www.cnblogs.com/greatverve/archive/2011/12/28/android-dip-dp-sp-pt-px.html  http://www.360 ...

  4. 23. Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. = ...

  5. SQLServer分页存储过程

    创建存贮过程: Create PROCEDURE [dbo].[UP_GetRecordByPage]@tblName   varchar(255),       -- 表名@fldName varc ...

  6. DBA_FND Load程式迁移工具介绍和应用(案例)

    2014-06-10 Created By BaoXinjian

  7. Red hat 5挂载U盘

    装在虚拟机上的Linux 一.挂载U盘                                                                                  ...

  8. Studio右键选项中没有Git?

    从Git clone一个Project并打开后,都会习惯性的像使用Eclipse一样,选中工程右键,选择Git的对应版本控制选项. 如下图,你只看到了svn. 如何配置才能在右键选项中看到Git呢,我 ...

  9. java基础-final

  10. shell+Jenkins+jmeter集成

    参考http://www.cnblogs.com/ceshi2016/p/6025641.html 中除ant相关内容,shell替代ant循环执行jmeter脚本和将jtl转换为html job n ...