Leetcode 12——Integer to Roman
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.
拿到题目,分析,比较简单,除掉相应的基数单位,拼接起来就可以,不过要注意4,9这些特殊的表示。
先上我的代码吧;
public String intToRoman(int num){
StringBuffer sb=new StringBuffer();
while(num!=0){
if(num>=1000){
append(sb, "M", num/1000);
num=num%1000;
}else if(num>=500){
if(num>=900){
append(sb,"CM",1);
num=num-900;
}else{
append(sb,"D",num/500);
num=num%500;
}
}else if(num>=100){
if(num>=400){
append(sb,"CD",1);
num=num-400;
}else{
append(sb,"C",num/100);
num=num%100;
}
}else if(num>=50){
if(num>=90){
append(sb, "XC", 1);
num=num-90;
}else{
append(sb,"L",num/50);
num=num%50;
}
}else if(num>=10){
if(num>=40){
append(sb,"XL",1);
num=num-40;
}else{
append(sb,"X",num/10);
num=num%10;
}
}else if(num>=5){
if(num>=9){
append(sb,"IX",1);
num=num-9;
}else{
append(sb,"V",num/5);
num=num%5;
}
}else{
if(num==4){
append(sb,"IV",1);
num=num-4;
}else{
append(sb,"I",num);
num=0;
}
}
}
return sb.toString();
}
public static void append(StringBuffer sb,String str,int times){
for(int i=0;i<times;i++){
sb.append(str);
}
}
但是呢,当我A掉之后,再去看这上面的第一个答案,跪了,又快又简单。用数组表示要放的数字。
public static String intToRoman(int num) {
String M[] = {"", "M", "MM", "MMM"};
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}
Leetcode 12——Integer to Roman的更多相关文章
- 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[] = {"", ...
- [leetcode] 12. Integer to Roman
关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...
随机推荐
- 2016四川省赛 Floyd-Warshall
这题真的有毒 首先你忽略 N-M < 100 的条件你就gg吧 其次就算你知道了怎么做之后 还有可能因为写vector或者各种常数大的原因被卡 #include<iostream> ...
- 异常-----freemarker.template.TemplateException: Macro select has no such argument
1.错误描述 六月 25, 2014 11:32:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...
- 改造 vue-cli 脚手架
改造 vue-cli 脚手架 注意,这里使用的 vue-cli 的版本为 2.8.2,不同版本的 vue-cli 可能会有些许不一样. 一.配置多页面 项目开发目录: 需要注意一点,每个页面的文件夹命 ...
- WebForm 生成并显示二维码
Generate and display QRCode in WebForm. 项目引用 QRCoder生成并显示 QRCode 项目引用 QRCoder How to use QRCoder Via ...
- IDM使用入门
IDM使用入门 Software IDM介绍 基本使用 浏览器集成 选项设置 进阶使用 IDM+百度云外链 IDM介绍 Internet Download Manager(IDM) is a tool ...
- 九九乘法表的实现--JAVA基础
JAVA算法实现:输出九九乘法表 Jiujiu.java: package com.qkys.www; public class Jiujiu { public static void main(St ...
- spring整合JMS
浏览博客时看到大神写的,直接转载过来收藏了.原文地址:http://elim.iteye.com/blog/1893038
- Java解析YAML和Android解析YAML
一.Java解析YAML 1. API的选择 一般分两种:Jyaml和snakeYAML.(Jyaml下载地址:http://download.csdn.net/detail/dgssfgfs/847 ...
- 都是SCI惹的祸?
都是SCI惹的祸? 过去只知道地质学家需要跋山涉水寻找宝藏,最近同一位海外归来的学者谈起,方知少数其它领域的科研人员,也"跋山涉水",在内地研究机构寻找可以写好文章的研究成果,不管 ...
- 【HDU4622】Reincarnation(后缀自动机)
[HDU4622]Reincarnation(后缀自动机) 题面 Vjudge 题意:给定一个串,每次询问l~r组成的子串的不同子串个数 题解 看到字符串的大小很小 而询问数太多 所以我们预处理任意的 ...