关于罗马数字:

I: 1
V: 5
X: 10
L: 50
C: 100
D: 500
M: 1000

字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:
I: 1, II: 2, III: 3, IV: 4
C: 100, CC: 200, CCC: 300, CD: 400

提取每一位digit,然后convert to 罗马数字

public class Solution {
private static char[][] chars = {{'I', 'V'}, {'X', 'L'}, {'C', 'D'}, {'M', 'O'}};
public String intToRoman(int num) {
int copy_num = num;
int count = 0;
StringBuilder result = new StringBuilder();
while (copy_num != 0) {
int x = copy_num / 10;
int digit = copy_num - x * 10;
switch (digit) {
case 3: result.append(chars[count][0]);
case 2: result.append(chars[count][0]);
case 1: result.append(chars[count][0]);
case 0: break;
case 4: result.append(chars[count][1]); result.append(chars[count][0]); break;
case 8: result.append(chars[count][0]);
case 7: result.append(chars[count][0]);
case 6: result.append(chars[count][0]);
case 5: result.append(chars[count][1]); break;
case 9: result.append(chars[count + 1][0]); result.append(chars[count][0]); break;
}
count++;
copy_num = x;
} result = result.reverse();
return result.toString();
}
}

[leetcode] 12. Integer to Roman的更多相关文章

  1. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  2. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  3. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  4. [LeetCode] 12. Integer to Roman ☆☆

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  5. [LeetCode] 12. Integer to Roman 整数转为罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  6. Java [leetcode 12] Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  7. [leetcode]12. Integer to Roman整数转罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  8. LeetCode——12. Integer to Roman

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

  9. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

随机推荐

  1. ResourceManager里面Trackingui需要手动该ip

    C:\Windows\System32\drivers\etc 这个路径下配置了ip和主机名,不过是大小写,ping不同,不论ping大小写还是全部小写都不行,我看地址栏是小写所以想着把hosts里C ...

  2. Git初级实践教程(图文)

    关于Git Git的由来 Linux 的创始人 Linus Torvalds 在 2005 年开发了 Git 的原型程序.当时,由于在 Linux 内核开发中使用的既有版本管理系统的开发方许可证发生了 ...

  3. ckeditor+angularjs directive

    var cmsPlus = angular.module('cmsPlus', []); cmsPlus.directive('ckEditor', function() { return { req ...

  4. microsoft docx document operation with Java POI library

    microsoft docx document operation with Java POI library combine multiple docx document into one docu ...

  5. Linux 内核数据结构:Linux 双向链表

    Linux 内核提供一套双向链表的实现,你可以在 include/linux/list.h 中找到.我们以双向链表着手开始介绍 Linux 内核中的数据结构 ,因为这个是在 Linux 内核中使用最为 ...

  6. Html特殊字符表

    原始字符 entity 原始字符 entity " " & & ' ' < < > >     ¡ ¡ ¢ ¢ £ £ ¤ ¤ ¥ ¥ ¦ ...

  7. 【原】理解javascript中的this

    最近的文章基本都是总结javascript基础内容的,因为我觉得这些东西很重要.而且很多时候你觉得你理解了,其实并没有你自认为的那么理解.十月份没怎么写文章,因为国庆出去玩的比较久,心变野了,现在是时 ...

  8. Kakfa重连测试

    在Kafak已启动的情况下: 发送端首次连接大概耗时400毫秒.后续消息发送都在1毫秒以下. 接收端首次连接大概耗时400-7000毫秒.后续消息接收都在1毫秒以下.(具体时间与topic中存留的消息 ...

  9. 【转】精选30个优秀的CSS技术和实例

    今天,我为大家收集精选了30个使用纯CSS完成的强大实践的优秀CSS技术和实例,您将在这里发现很多与众不同的技术,比如:图片集.阴影效果.可扩展按钮.菜单等-这些实例都是使用纯CSS和HTML实现的. ...

  10. Solr学习总结(七)Solr搜索引擎的整体架构

    经过前面一段时间的努力,终于把我所知道的关于solr 的内容都总结完了.前面讲到了solr 的安装配置,web管理后台的使用,solr 的查询参数和查询语法,还说到了solr的客户端 solrnet  ...