题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
翻译:
给一个整数,把他转换为罗马数字输出。
这道题是简单的字符转换的问题。只要了解罗马数字代表的含义即可。
罗马数字里面只有 1,5,10. 1到3之间用重复的1表示,4用IV 90用XC 依次类推。
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
代码:
public static String intToRoman(int num)
{
String str ="";
String roman[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int number[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
for(int i = 0 ;num!=0 ;i++)
{
while(num >= number[i])
{
num-=number[i];
str+=roman[i];
}
}
return str;
}
这道题比较简单。只要了解罗马数字代表的 意思即可。
分享到:
相关推荐
13. 罗马数字转整数 Roman to Integer用哈希存储映射字符--->对映的值对字符串的字符挨个判断,考虑下一个字符如果下一个字符大于当前字符,su
leetcode 跳跃 LeetCode Solved by Python easy/middle/hard:15/36/5 1. Two Sum ...Integer 整数反转 ...to Integer ...字符串转换整数 ...12. Integer to ...整数转罗马数字 13. Roman to Integer 罗马数字转
整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G
lru缓存leetcode 力码 ...整数转罗马 (30_Medium) LeetCode 13 : Roman to Integer (249_Easy) LeetCode 14 : 最长公共前缀 (250_Easy) LeetCode 15 : 3Sum (271_Medium) LeetCode 17 : 电话号码的
罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search ...
leetcode中文版 ...罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个有序链表 lin
leetcode给单链表加一js实现 ...Integer:翻转整数 09 Palindrome Number:回文数 11 Container With Most Water:双指针搜索的典型例子 12 Integer to Roman: 将阿拉伯数字转换为罗马数字 38 Count and Say:
(/problems/integer-to-english-words/) 22.1% 难278 25.4% 容易17 34.7% 中91 19.7% 中10 正则表达式匹配 (/problems/regular-expression-matching/) 24.1% 难253 38.9% 中15 21.6% 中277 寻找名人 (/problems/...
leetcode 答案力码 做LeetCode 1.【二和】(Array & Hash Table) (Easy) 20190911 完成 2.【加两个数】(链表&数学...12.【整数转罗马】(中)太难回答 13.【Roman To Integer】(中) 20181106完成的IIV是非法的? 14.
丢失的最小正整数leetcode LeetCode Note 7. Reverse Integer:对于检测最大值,没有合适把握,无法选择正确的格式对值进行存储,导致丢失部分数据。同时老是用/10导致无法确定上界,消耗了时间去计算。改为用从下往...
Integer.js 14 最长公共前缀.js 15 3Sum.js 16 3Sum Closest.js 电话号码的 17 个字母组合.js 19 从 List.js 的末尾删除第 N 个节点 20 个有效括号.js 21 合并两个有序Lists.js 22 生成括号.js 23 合并 k 排序 Lists...
这是带有js的leetcode解决方案。 我已经对一些leetcode问题做了一些解释。 您可以访问来查看这些说明。 问题 解释 来源 [查看源代码](./ js / 1...转换为Roman.js的整数) 13.罗马到整数 查看说明 [查看源代码]
integer-to-roman.go 欢迎提交 解题思路 algorithm programming 1.E: two-sum:两数之和 在两个数组找到和为x的两个数的下标 O(n), range数组, 判断targetNum(sum-rangeNum)是否已经在取出的map中 2.M: add-two-...
罗马数字转整数(RomanToInteger) 两数之和(sumInArray) 有序数组的平方(squaresOfASortedArray) 卡牌分组(xOfAKindInADeckOfCards) 排序数组去除重复项(removeDuplicatesFromSortedArray) 冒泡...