13-Integer to Roman-字符串相关-HashMap
来源:互联网 发布:淘宝儿童女装14岁 编辑:程序博客网 时间:2024/05/01 19:02
【题目】
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
【解析】
和上一题对偶,同样考查HashMap的使用
我的思路是先给HashMap赋值,然后获取int字符类型每一位的值,换算成可以查找HashMap对应值的类型后相加即可。
【代码】
public class Solution {
public String intToRoman(int num) {
Map<Integer,String> map = new HashMap<Integer,String>();
String[] roman = {"","I","II","III","IV","V","VI","VII","VIII","IX",
"X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
"C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
"M","MM","MMM"};
int[] intnum={0,1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,200,300,
400,500,600,700,800,900,1000,2000,3000};
for(int i=0;i<31;i++){
map.put(intnum[i], roman[i]);
}
int g,s,b,q;
g=num%10;
s=num/10%10*10;
b=num/100%10*100;
q=num/1000%10*1000;
String result=map.get(q)+map.get(b)+map.get(s)+map.get(g);
return result;
}
}
- 13-Integer to Roman-字符串相关-HashMap
- 12-Roman to Integer-字符串相关-HashMap
- 13:Roman to Integer【数学】【字符串】
- leetcode---roman-to-integer---字符串
- 13、Roman to Integer
- 13、Roman to Integer
- 13Roman to Integer
- #13 Roman to Integer
- 13Roman to Integer
- 13 Roman to Integer
- 13 Roman to Integer
- *13 Roman to Integer
- 13、Roman to Integer
- 13 Roman to Integer
- 【13】Roman to Integer
- 13 Roman to Integer
- 13-Roman to Integer
- Leetcode[12-13] Roman to Integer & Integer to Roman(Java)
- 使用GIMP从连排png文件中抽取单一图片的方法
- 管理感悟26条——项目管理/研发管理
- IEEE和SCI等的通俗简介
- /dev/fb0入门练习(linux FrameBuffer)
- 杭电ACM 2054:A == B ?
- 13-Integer to Roman-字符串相关-HashMap
- 安卓APP动态调试-IDA实用攻略
- 获取系统时间
- LODOP实现打印
- Objective-c NSFileManager
- mariadb init
- 欢迎您在新浪博客安家
- WebService与WebApplication的区别
- Android 快速开发系列 ORMLite 框架最佳实践