leetcode — integer-to-roman
/**
 * 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的更多相关文章
- LeetCode: Integer to Roman  解题报告
		Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ... 
- [LeetCode] Integer to Roman 整数转化成罗马数字
		Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ... 
- Leetcode Integer to Roman
		Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ... 
- LeetCode——Integer to Roman
		Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the r ... 
- leetcode Integer to Roman python
		class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str &qu ... 
- [LeetCode][Python]Integer to Roman
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ... 
- Integer to Roman - LeetCode
		目录 题目链接 注意点 解法 小结 题目链接 Integer to Roman - LeetCode 注意点 考虑输入为0的情况 解法 解法一:从大到小考虑1000,900,500,400,100,9 ... 
- 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 ... 
- LeetCode:12. Roman to Integer (Easy)
		1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ... 
- leetCode练题——12. Integer to Roman
		1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ... 
随机推荐
- Luogu3587[POI2015]POD - hash + 单调队列
			Solution 还是去看了题解. 感谢大佬的博客→ 题解传送门 是一道思路比较新的题. 搞一个前缀和, 记录前 $i$ 个位置每种颜色的出现次数, 如果位置 $i$ 是 颜色 $a[i]$ 的最后 ... 
- 别人的Linux私房菜(6)文件权限与目录配置
			账号与一般身份用户存放在/etc/passwd文件中 个人密码存放在/etc/shadow文件中 Linux所有组名存放在/etc/group中 ls -al查看所有信息并显示权限等 文件权限的10字 ... 
- usb协议栈学习笔记
			1.usb 集线器为什么一般都是只有4个扩展口? PC的根集线器可为每个A型连接器提供5V.500mA电源.一个总线供电的外部集线器可为每个端口提供100mA电流.由于USB为为外部集线器电路分配10 ... 
- you-get 安装和用法
			以windows为例 安装 从https://github.com/soimort/you-get/releases/latest下载*-full.7z,解压后在cmd中切换至目录下执行you-get ... 
- 卷积在深度学习中的作用(转自http://timdettmers.com/2015/03/26/convolution-deep-learning/)
			卷积可能是现在深入学习中最重要的概念.卷积网络和卷积网络将深度学习推向了几乎所有机器学习任务的最前沿.但是,卷积如此强大呢?它是如何工作的?在这篇博客文章中,我将解释卷积并将其与其他概念联系起来,以帮 ... 
- 带权单源最短路发[稠密图](Dijkstra)
			对于稠密图,采用邻接矩阵较为合适 所以我们先构建一个邻接矩阵 typedef int Vertex; typedef int WeightType; //图 typedef struct MyGrap ... 
- 【接口时序】4、SPI总线的原理与Verilog实现
			一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 硬件平台: 1. FPGA型号:Xilinx公 ... 
- IOS - 修改APP桌面名称为中文名称!
			1,修改“Display Name”为想要的中文. 2,修改“bundle display name”为想要的中文. 
- 在源文件(.c)和头文件(.h)中声明和定义的区别——C语言
			最近在看多文件编程的时候遇到的一个问题,本来以为理解了声明和定义的区别(然而并没有····),也算是重新认识了一次声明和定义,下面上代码 情形一:在源文件(.c)中 相信大部分读者对声明和定义的理解是 ... 
- 枚举类型enum详解——C语言
			enum enum是C语言中的一个关键字,enum叫枚举数据类型,枚举数据类型描述的是一组整型值的集合(这句话其实不太妥当),枚举型是预处理指令#define的替代,枚举和宏其实非常类似,宏在预处理阶 ... 
