/**
* Source : https://oj.leetcode.com/problems/integer-to-roman/
*
* Created by lverpeng on 2017/7/10.
*
* Given an integer, convert it to a roman numeral.
*
* Input is guaranteed to be within the range from 1 to 3999.
*
*/
public class IntegerToRoman {
private static final int[] value = new int[] {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
private static final String[] symbol = new String[] {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; /**
* 罗马数字只有1、5、10、50、100、500、1000,将integer从左到右循环,num大于当前的罗马符号的时候减1,直到小于当前罗马符号的时候进行下一次循环
* 特殊情况,4、9、40、90、400、900也需要加入符号表考虑
*
* @param num
* @return
*/
public String integerToRoamn (int num) {
if (num < 1 || num > 3999) {
return null;
}
String roman = "";
for (int i = 0; num != 0; i++ ) {
while (num >= value[i]) {
num = num - value[i];
roman += symbol[i];
}
}
return roman;
} public static void main(String[] args) {
IntegerToRoman integerToRoman = new IntegerToRoman();
System.out.println(integerToRoman.integerToRoamn(1000) + "------M");
System.out.println(integerToRoman.integerToRoamn(1038) + "------MXXXVIII");
System.out.println(integerToRoman.integerToRoamn(1) + "------I");
System.out.println(integerToRoman.integerToRoamn(2) + "------II");
System.out.println(integerToRoman.integerToRoamn(3) + "------III");
System.out.println(integerToRoman.integerToRoamn(4) + "------IV");
System.out.println(integerToRoman.integerToRoamn(3094) + "------MMMXCIV");
} }

leetcode — integer-to-roman的更多相关文章

  1. LeetCode: Integer to Roman 解题报告

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

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

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

  3. Leetcode Integer to Roman

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

  4. LeetCode——Integer to Roman

    Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the r ...

  5. leetcode Integer to Roman python

    class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str &qu ...

  6. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  7. Integer to Roman - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Integer to Roman - LeetCode 注意点 考虑输入为0的情况 解法 解法一:从大到小考虑1000,900,500,400,100,9 ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

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

  9. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  10. leetCode练题——12. Integer to Roman

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

随机推荐

  1. 时间戳转中国人能看得懂的日期格式 yy-mm-dd

    很多项目都会用到时间戳的转换 说实话  我现在的这家公司超级好 因为后太要求传数据的时候竟然可以是时间戳的格式 我觉得我好幸福 哈哈哈 不过 等后台转给你数据的时候很多时候都是时间戳 这时候就得前端转 ...

  2. Egret中使用P2物理引擎

    游戏中的对象按照物理规律移动,体现重力.引力.反作用力.加速度等物体特性,实现自由落体.摇摆运动.抛物线运动,以及物理碰撞现象的模拟.用于模拟物理碰撞.物理运动的引擎称为物理引擎. 来自瑞典斯德哥尔摩 ...

  3. boost asio 学习(四)使用strand将任务排序

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=5 4. Seri ...

  4. JS基础-分支结构-循环-数组

    1.分支结构  1.if结构    语法:    if(条件){       语句块:    }    注意:      1.条件尽量是boolean的,如果不是boolean的,以下条件值,会当做f ...

  5. RDS MySQL InnoDB 锁等待和锁等待超时的处理

    https://help.aliyun.com/knowledge_detail/41705.html 1. Innodb 引擎表行锁等待和等待超时发生的场景 2.Innodb 引擎行锁等待情况的处理 ...

  6. john and hydra using de-ice1.100

    配置IP  ipconfig etho 192.168.179.111 http://192.168.179.111/index2.php curl http://192.168.179.111/in ...

  7. vue路由跳转到指定页面

    1.this.$router.push({name:'Home'}) 2.this.$router.push({path:'/view'}) 3.this.$router.replace({name: ...

  8. 7. The British Thached Roof 英国的茅草屋顶

    7. The British Thached Roof 英国的茅草屋顶 (1) The view over a valley of a tiny village with thatchd roof c ...

  9. DataBrewery Cubes 连接Kylin

    问题背景 Kylin作为一个极其优秀的MOLAP,提供了完整的Cube创建.更新流程.同时提供了Sql查询.功能上看没有问题,但是在提供查询服务的时候还是有些不友好. sql查询需要常常需要关联Hiv ...

  10. JavaScrip继承图文总结

    JavaScript有多种继承模式,总结起来用到的方法有:原型链的传递.构造函数的借用.对象的复制.     这篇文章讲得很清晰,让我们明白:所有JS对象源于null,并通过原型指针和原型对象来实现继 ...