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, ...
随机推荐
- kubernetes namespace Terminating
1.kubectl get namespace annoying-namespace-to-delete -o json > tmp.jsonthen edit tmp.json and rem ...
- POI2014解题报告
穷酸博主没有bz权限号, 也不会去$poi$官网, 在某咕刷的$poi$,按照某咕的难度排序刷, poi~ $Luogu3572 PTA-Little Bird$ 单调队列, 队列内按照 步数为第一关 ...
- Python之ftp服务器
今天把做的ftp服务器过程总结一下,先看看要求 一.需求 1. 用户加密认证 2. 允许同时多用户登录 3. 每个用户有自己的家目录 ,且只能访问自己的家目录 4. 对用户进行磁盘配额,每个用户的可用 ...
- [IBM][CLI Driver] SQL0270N 函数不受支持(原因码:"75")。 SQLSTATE=42997
db2 update dbm cfg using FEDERATED yes 与 自动维护 (AUTO_MAINT) = ON 自动数据库备份 (AUTO_DB_BACKUP) = OFF 自动表 ...
- solr7.7.0搜索引擎使用(一)(下载安装)
一.下载安装 可以直接在官网下载地址:https://lucene.apache.org/solr/ 解压之后,目录结构如下图,bin里边提供部署的文件,contrib提供额外的jar包,docs提供 ...
- SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase] 错误
在一次改bug的过程,爆出了数据库错误,但是一看后面控制台,并没有爆出以前的具体的数据库错误的原因,而是 SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, In ...
- modal 永久移除遮盖层
样式中直接加入: .modal-backdrop { opacity: 0 !important; filter: alpha(opacity=0) !important;}
- Re:uxul
Re: Unbelieveable eXperience of University Life
- asp.net对接拼多多
asp.net对接拼多多视频地址:https://www.bilibili.com/video/av43512047/?p=7
- Android开发工程师文集-1 小时学会Widget小组件开发
前言 大家好,给大家带来Android开发工程师文集-1 小时学会Widget小组件开发的概述,希望你们喜欢 学会用Widget (小组件) Widget小组件很方便,很快捷,可以个性化,自己定制,相 ...