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, ...
随机推荐
- MySQL数据查询之单表查询
单表查询 简单查询 - 创建表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCREMEN ...
- [Selenium] 在Chrome的开发者工具中验证检查XPath/CSS selectors
Evaluate and validate XPath/CSS selectors in Chrome Developer Tools Method 1 : From Elements panel U ...
- nginx配置备忘
一.本地测试环境配置 upstream gongsibao{ server ; server ; #fair; } server { listen ; server_name ubuntu00.xus ...
- O365 Manager Plus帮助台委派介绍
O365 Manager Plus帮助台委派介绍 虽然Office 365允许您在全球任何地方工作,但它提供的管理功能十分不足.当一个组织分布在多个国家/地区时,一个管理员很难单独管理所有用户和邮箱. ...
- getObjectURL 上传图片预览
js 函数 function getObjectURL(file) { var url = null ; if (window.createObjectURL!=undefined) { ...
- 根据Excel模板存储数据,并下载
@RequestMapping("/exportList") @ResponseBody public Map<String, Object> exportList(H ...
- python之路(三)-深浅拷贝
深浅拷贝用法来自copy模块. 导入模块:import copy 浅拷贝:copy.copy 深拷贝:deepcopy 字面理解:浅拷贝指仅仅拷贝数据集合的第一层数据,深拷贝指拷贝数据集合的所有层.所 ...
- Spring Cloud之踩坑01 -- Eureka高可用配置
转载:https://blog.csdn.net/dear_Alice_moon/article/details/79373955 问题描述: 在进行Eureka高可用配置时,控制台一直出现“.... ...
- [ 9.29 ]CF每日一题系列—— 765B字符串规律
Description: 遇到了ogo可以变成***如果ogo后面有go统统忽略,输出结果 Solution: 哎如果我一开始对题意的解读如上的话,就不会被整的那么麻烦了 Code: #include ...
- ZZ 使用Jenkins配置Git+Maven的自动化构建
http://blog.csdn.net/xlgen157387/article/details/50353317 Jenkins是帮我们将代码进行统一的编译打包.还可以放到tomcat容器中进行发布 ...