Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "I"); map.put(5, "V"); map.put(10, "X"); map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M"); int base = 1000; while(base >= 1) { int n = num/base; if(n==4) { sb.append(map.get(base)).append(map.get(base*5)); } else if(n==9) { sb.append(map.get(base)).append(map.get(base*10)); } else { if(n>=5) { sb.append(map.get(base*5)); n -= 5; } for(int i=0; i<n; i++) { sb.append(map.get(base)); } } num %= base; base /= 10; } return sb.toString(); }
C++代码更简洁一些:
string intToRoman(int num) { unordered_map<int, char> map = {{1,'I'},{5,'V'},{10,'X'},{50,'L'},{100,'C'},{500,'D'},{1000, 'M'}}; string res; vector<int> list = {1000, 100, 10, 1}; for(auto n:list) { int d = num/n; num %= n; if(d == 0) continue; if(d < 4) { res.append(d, map[n]); } else if(d == 4) { res += map[n]; res += map[n*5]; } else if(d < 9) { res += map[n*5]; if(d > 5) res.append(d-5, map[n]); } else if(d == 9) { res += map[n]; res += map[n*10]; } } return res; }
相关推荐
leetcode-integer_to_roman
LeetCode Roman to Integer解决方案
12.integer-to-roman,自动创建文件夹和文件并使用vim打开文件(如下文件内容是自动生成的,根据剪贴板中的代码) 在 Solution 类中填写代码,保存退出 在终端输入leetcode commit ,即可将Solution类的代码复制到系统...
leetcode上Roman to Integer的完整C++代码,已被accepted
12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter Combinations of a Phone Number 18.4Sum 19.Remove Nth Node From End
12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G
leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方...
leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- ...
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 005 Longest Palindromic Substring 006 ZigZag Conversion 007 Reverse Integer 008 ...
leetcode 跳跃 LeetCode Solved by Python easy/middle/hard:15/36/5 1. Two Sum 两数之和 2. Add Two ...Integer ...to Integer ...12. Integer to Roman 整数转罗马数字 13. Roman to Integer 罗马数字转
分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...
leetcode 答案 LeetCode My LeetCode solution List 4. Longest Substring Without Repeating Characters: ...to ...12. Integer to Roman - >using this radix: mod = ['M','CM','D','CD','C','XC','L','XL'
Roman to Integer Easy #21 Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray ...
leetcode提交记录消失解决leetcode问题 ...https://leetcode.com/problems/roman-to-integer/description/ first-submission-successful : yes 2018-05-16 : - id : 172 type : math difficulty : easy url : ...
13-roman-to-integer 14 cargo run --bin 14-longest-common-prefix 17 cargo run --bin 17-letter-combinations-of-a-phone-number 20 cargo run --bin 20-valid-parentheses 21 cargo run --bin 21-merge-two-...
Integer 运行时间:52 毫秒内存使用:14.1 MB 14. Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 ...
leetcode 答案力码 做LeetCode 1.【二和】(Array & Hash Table) (Easy) 20190911 完成 2.【加两个数】(链表&数学...12.【整数转罗马】(中)太难回答 13.【Roman To Integer】(中) 20181106完成的IIV是非法的? 14.
问题/数组和字符串/13.roman_to_integer.md) [26. Remove Duplicates from Sorted Array](Leetcode Problems/Array and String/26_remove_duplicates_from_sorted_array.md) [(雅虎)139。 Word Break](Leetcode ...
Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长...
罗马数字转整数(RomanToInteger) 两数之和(sumInArray) 有序数组的平方(squaresOfASortedArray) 卡牌分组(xOfAKindInADeckOfCards) 排序数组去除重复项(removeDuplicatesFromSortedArray) 冒泡...