【LeetCode】Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


public class Solution {
public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
if(num==0)
return sb.toString();
while(num!=0){
if(num>=1000){
int temp = num/1000;
for(int i=0;i<temp;i++)
sb.append("M");
num = num%1000;
continue;
}
if(num>=500){
if(num/900==1){
sb.append("CM");
num=num%900;
continue;
}
num = num%500;
sb.append("D");

}
if(num>=100){
if(num/400==1){
sb.append("CD");
num=num%400;
continue;
}
int temp = num/100;
for(int i=0;i<temp;i++)
sb.append("C");
num=num%100;
continue;
}
if(num>=50){
if(num/90==1){
sb.append("XC");
num=num%90;
continue;
}
num=num%50;
sb.append("L");
continue;
}
if(num>=10){
if(num/40==1){
sb.append("XL");
num=num%40;
continue;
}
int temp = num/10;
for(int i=0;i<temp;i++)
sb.append("X");
num=num%10;
continue;
}
if(num>=5){
if(num/9==1){
sb.append("IX");
num=num%9;
continue;
}
num=num%5;
sb.append("V");
continue;
}
if(num>=1){
if(num/4==1){
sb.append("IV");
num=num%4;
continue;
}
int temp = num;
for(int i=0;i<temp;i++)
sb.append("I");
num=0;
continue;
}
}
return sb.toString();

}
}

【LeetCode】Integer to Roman,布布扣,bubuko.com

时间: 2024-12-05 07:48:49

【LeetCode】Integer to Roman的相关文章

【LeetCode】Integer to Roman 和 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Or, Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. [罗马数字] 1~9: {"I", "II", "III", "IV", "V"

【LeetCode】Integer to Roman (2 solutions)

Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 逐区间做处理. 解法一:非递归 class Solution { public: string intToRoman(int num) { string ret; //M<-->1000 while(num >= 1000) { ret +=

【Leetcode】Integer Break

题目链接:https://leetcode.com/problems/integer-break/ 题目: Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, re

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【leetcode刷题笔记】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解:基本的罗马字符和数字对应如下表所示: 罗马字符 数字 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表: 罗马字符 数字 IV 4 IX

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

LeetCode 012 Integer to Roman

[题目] Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. [题意] 给定一个整数,将其表示成罗马数字 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 罗马数组数规则: 基本数字Ⅰ.X .C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成