【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[] = {"", ...
随机推荐
- 在VS 2010中使用 VS2013的解决方案
本文转载自:http://blog.csdn.net/u011543589/article/details/25563351 今天要用VS2010打开VS2013,一直觉得VS2010到VS2012只 ...
- Learning Puppet — Manifests
Begin In a text editor — vim, emacs, or nano — create a file with the following contents and filenam ...
- golang的的模板引擎之pongo2
https://github.com/flosch/pongo2 beego的扩展包 https://github.com/yansuan/beego-pongo2 gin的扩展包 https://g ...
- Akka(二) - Future
1. future的所有方法都是非阻塞立即返回的 (1)future都要有TimeOut和ExecutionContextExecutor这2个隐士参数 (2)打印future object Hell ...
- My to do 12.25
Merry Christmas 新的一年要来了,参加工作转眼也快半年了.回顾以往,多逢贵人.不忘初心,感慨良多.祝所有的朋友都能幸福,愿望都可以实现,日子越过越好~ Look Back 作为GH加入北 ...
- PLSQL_查询SQL的执行次数和频率(案例)
2014-12-25 Created By BaoXinjian
- PLSQL_PLSQL中DML/DDL/DCL的概念和区分(概念)
2014-06-20 Created By BaoXinjian
- [rm] Linux 防止"rm -rf /" 误删除
一.缘由: 最近看到这则新闻,很是悲伤,因为我最近也在用ansible:然而这一错误源自Ansible上糟糕的代码设计,这款Linux实用工具被用于在多台不同服务器上自动执行脚本. 开发者解释到,实际 ...
- GC学习笔记
GC学习笔记 这是我公司同事的GC学习笔记,写得蛮详细的,由浅入深,循序渐进,让人一看就懂,特转到这里. 一.GC特性以及各种GC的选择 1.垃圾回收器的特性 2.对垃圾回收器的选择 2.1 连续 V ...
- 用happen-before规则重新审视DCL(转)
编写Java多线程程序一直以来都是一件十分困难的事,多线程程序的bug很难测试,DCL(Double Check Lock)就是一个典型,因此对多线程安全的理论分析就显得十分重要,当然这决不是说对多线 ...