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; }
相关推荐
C语言-leetcode题解之12-integer-to-roman.c的实现,是编程学习者在提升数据结构和算法能力,以及深入理解C语言特性方面的一个有益尝试。通过对该题目的学习和练习,可以有效增进对计算机科学基础概念的掌握,并对...
c c语言_leetcode 0013_roman_to_integer.zip
java入门 java_leetcode题解之013_Roman_to_Integer
java入门 java_leetcode题解之012_Integer_to_Roman
本篇题解所涉及的文件名为“13-roman-to-integer.c”,这是C语言编写的源代码文件,专门针对LeetCode上编号为13的题目:“Roman to Integer”。这道题目要求编写一个函数,该函数接收一个表示罗马数字的字符串作为...
在leetcode网站上,这个问题被标记为“Medium”难度,主要考察程序员对于数值到特定表示法转换的理解,以及对数组、字符串操作的熟悉度。解决这道题不仅需要了解罗马数字的基本规则,还应该具备将复杂问题分解为较小...
12. 实用性:该算法不仅适用于解决LeetCode这类算法题,还能够应用到需要将罗马数字转换为整数的实际场景中,比如处理某些历史数据或者与老系统交互时遇到的罗马数字。 13. 技术深度:该问题的解决方案涉及到了...
LeetCode Roman to Integer解决方案
标题 "leetcode-integer_to_roman" 指的是一个关于 LeetCode 上的编程挑战,该挑战要求将整数转换为罗马数字。这是一个典型的字符串处理和算法设计问题,常见于计算机科学和技术面试中,用于测试候选人的逻辑思维和...
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
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 罗马数字转
12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G
- Integer to Roman / Roman to Integer: 这两个问题分别要求将整数转换为罗马数字,或者将罗马数字转换为整数。 - Longest Common Prefix: 找出一个字符串数组中所有字符串的最长公共前缀。 - 3Sum / 3Sum Closest ...
分割数组求最大差值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'