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, ...
随机推荐
- C# 文件copy和文件删除
C# 文件copy和文件删除 public bool CopyFile(string SourcePath, string CopyPathFoder) { bool bfg = false; if ...
- GitHub 错误解决
1. The file will have its original line endings in your working directory. git config --global core. ...
- 【原】storm组件(架构层面)
Strom集群遵循从主模式,主与从之间通过Zookeeper协作.架构层面上包括三个组件: 1) Nimbus Node 2)Supervisor Nodes 3)Zookeeper 其中Nimbus ...
- 基于数据库的自动化生成工具,自动生成JavaBean、数据库文档、框架代码等(v5.8.8版)
TableGo v5.8.8版震撼发布,此次版本更新如下: 1.新增两个扩展字段,用于生成自定义模板时使用. 2.自定义模板新增模板目录,可以选择不同分类目录下的模 ...
- java——基础语法
java基础语法 1.关键字:java赋予特殊含义的单词. 2.标识符:程序中开发人员自定义的名词,例如:类名,函数名,变量名(注意事项:①不能以阿拉伯数字开头②不能采用关键字). 3.常量:固定的数 ...
- Keras常见问题及解答
Keras官方中文版文档 如何引用 Keras? 如何在 GPU 上运行 Keras? 如何在多 GPU 上运行 Keras 模型? "sample", "batch&q ...
- 使用Spring-hadoop小结
SpringHadoop是通过Spring框架来调用hdfs,跟直接调用hdfs的最大的不同区别是Spring通过依赖注入的方式生成操作hdfs所需要的configuration和filesystem ...
- UVA10294 Arif in Dhaka (群论,Polya定理)
UVA10294 Arif in Dhaka (群论,Polya定理) 题意 : 给你一个长为\(n\)的项链和手镯,每个珠子有\(m\)种颜色. 两个手镯定义为相同,即它们通过翻转和旋转得到一样的手 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- Bitset([HZOI 2015]偏序++)
Bitset简介 下面介绍C++ STL 中一个非常有用的东西: Bitset 类似于二进制状压,它可以把信息转化成一个01串存储起来 定义方法: 首先要#include<bitset>或 ...