/**
* 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. Linux驱动之一个简单的输入子系统程序编写

    的在Linux驱动之输入子系统简析已经分析过了输入子系统的构成,它是由设备层.核心层.事件层共同组成的.其中核心层提供一些设备层与事件层公用的函数,比如说注册函数.反注册函数.事件到来的处理函数等等: ...

  2. Linux下所有命令失效的解决方法

    今天在设置环境变量时,一不小心,设置错了,系统中的所有命令全部失效了,可把我急坏了,下面用一条命令可解决: 解决办法:重新设置环境变量PATH export PATH=/usr/sbin:/usr/b ...

  3. TerraGate SFS Manager配置时权限设置问题

    配置SFS Manager时出现以下错误:      "windows account(*\ASPNET) that does not have sufficient permissions ...

  4. 获取IP地址方法

    function getip() {     static $ip = '';     $ip = $_SERVER['REMOTE_ADDR'];     if(isset($_SERVER['HT ...

  5. ABP框架系列之二十七:(Feature-Management-特征管理)

    Introduction Most SaaS (multi-tenant) applications have editions (packages) those have different fea ...

  6. The Python Challenge 0-4

    The Python Challenge 0-4 项目地址:http://www.pythonchallenge.com/ Level-0 提示Hint: try to change the URL ...

  7. RxSwift学习笔记9:amb/tabkeWhile/tabkeUntil/skipWhile/skipUntil

    //amb基本介绍 //当传入多个 Observables 到 amb 操作符时,它将取第一个发出元素或产生事件的 Observable,然后只发出它的元素. //并忽略掉其他的 Observable ...

  8. Dynamic Programming | Set 3 (Longest Increasing Subsequence)

    在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Opti ...

  9. 在Azure DevOps Server (TFS 2019) 流水线传递参数

    变量概述 在Azure DevOps Server的流水线中,变量是衔接不同任务和不通代理之间的桥梁,它可以使相对松散.各自独立的任务之间相关影响并共享数据.在流水线中使用变量,可以在各任务之间相互调 ...

  10. VB.NET 定义多行文本字符的几种方式

    vbCrLf 在 .NET 刚刚推出的时候,VB作为一款被微软用来"衬托"C#的语言,在许多细节设计上远不如C#方便. 比如在C#中写一个多行文本,就有各种方式: string s ...