`

LeetCode 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.

 

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 0013-roman-to-integer.zip

    c c语言_leetcode 0013_roman_to_integer.zip

    java-leetcode题解之013-Roman-to-Integer

    java入门 java_leetcode题解之013_Roman_to_Integer

    java-leetcode题解之012-Integer-to-Roman

    java入门 java_leetcode题解之012_Integer_to_Roman

    LeetCode Roman to Integer解决方案

    LeetCode Roman to Integer解决方案

    leetcode-integer_to_roman

    标题 "leetcode-integer_to_roman" 指的是一个关于 LeetCode 上的编程挑战,该挑战要求将整数转换为罗马数字。这是一个典型的字符串处理和算法设计问题,常见于计算机科学和技术面试中,用于测试候选人的逻辑思维和...

    leetcode中国-leetcode-local:leetcode本地

    12.integer-to-roman,自动创建文件夹和文件并使用vim打开文件(如下文件内容是自动生成的,根据剪贴板中的代码) 在 Solution 类中填写代码,保存退出 在终端输入leetcode commit ,即可将Solution类的代码复制到系统...

    Roman to Integer完整代码

    leetcode上Roman to Integer的完整C++代码,已被accepted

    leetcode338-LeetCode:LeetCode刷题总结

    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:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方...

    leetcode卡-LeetCode:LeetCode题解

    leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- ...

    leetcode530-algorithm:算法

    leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 005 Longest Palindromic Substring 006 ZigZag Conversion 007 Reverse Integer 008 ...

    leetcode跳跃-LeCode:乐科

    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:力码

    12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G

    _leetcode-python.pdf

    - Integer to Roman / Roman to Integer: 这两个问题分别要求将整数转换为罗马数字,或者将罗马数字转换为整数。 - Longest Common Prefix: 找出一个字符串数组中所有字符串的最长公共前缀。 - 3Sum / 3Sum Closest ...

    leetcode提交记录消失-leetcode-java:我对leetcode问题的解决方案

    leetcode提交记录消失解决leetcode问题 ...https://leetcode.com/problems/roman-to-integer/description/ first-submission-successful : yes 2018-05-16 : - id : 172 type : math difficulty : easy url : ...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...

    leetcode答案-LeetCode:我的力扣解决方案

    leetcode 答案 LeetCode My LeetCode solution List 4. Longest Substring Without Repeating Characters: ...to ...12. Integer to Roman - &gt;using this radix: mod = ['M','CM','D','CD','C','XC','L','XL'

    leetcode答案-LeetCode:Swift中的LeetCode

    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 ...

    gasstationleetcode-leetcode-rust:莱特代码休息

    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-...

    leetcode2-Algorithms-Practice:创建此repo是为了跟踪我在解决问题方面的进展

    Integer 运行时间:52 毫秒内存使用:14.1 MB 14. Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 ...

Global site tag (gtag.js) - Google Analytics