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, ...
随机推荐
- centos 7 安装redis 3.2.1
https://www.cnblogs.com/zuidongfeng/p/8032505.html 下载安装包: http://download.redis.io/releases/ 根据需要自己选 ...
- 设置textfield 文字左边距
默认情况下,当向textField输入文字时,文字会紧贴在textField左边框上.我们可以通过设置textField的leftView,设置一个只有宽度的leftView.这样还不够,因为默认le ...
- django by example 第四章 扩展 User 模型( model)
描述: RelatedObjectDoesNotExist at /account/edit/ User has no profile. 原因: 注意原书,要求新建一个账户才能使用.
- 基于akka-http搭建restfull框架
1.scala开发环境介绍 2.scala插件的demo模板介绍 3.akka-http提供demo研究 4.添加路由机制解析 package org.netsharp.rest import akk ...
- solr7.7.0搜索引擎使用(四)(搜索语法)
solr搜索语法 参数defType 指定用于处理查询语句(参数q的内容)的查询解析器,eg:defType=lucenesort 指定响应的排序方式:升序asc或降序desc.同时需要指定 ...
- 【转】机器学习在B2B的应用
原文地址:http://www.mbtmag.com/blog/2017/04/artificial-intelligence-making-it-work-industrial-companies? ...
- Python Day 3
阅读目录: 内容回顾: 变量(标识符)的命名规范: 常量: 格式化输入\输出: 注释: 基本数据类型: 运算符: ##内容回顾 1.语言的分类: -- 机器语言:直接编写0,1指令,直接能被硬件执行. ...
- python模块:time
# encoding: utf-8 # module time # from (built-in) # by generator 1.145 """ This modul ...
- 学以致用二十三-----shell脚本里调用脚本
当前脚本可以调用其他目录下的脚本,并可以直接使用其他脚本里的函数. 首先查看脚本目录 执行net_set.sh,同时执行colos.sh 并可直接使用 color.sh中的函数 net_set.sh ...
- SQL STUFF函数 拼接字符串 多列 合并成一列 转
关于和并列的 要这种效果. create table tb(idint, value varchar(10)) insert into tbvalues(1,'aa') insert into tbv ...