【leetcode】12. Integer to Roman

题目描述:

Given an integer, convert it to a roman numeral.

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

解题思路:

注意到用例比较少,所以采用以空间换时间的方法,把所有的结果列出,然后组合出输入值n的字符串即可。

具体代码:

 1 public class Solution {
 2     public static String intToRoman(int num) {
 3         String[][] array={
 4                 {"","I","II","III","IV","V","VI","VII","VIII","IX"},//0-9
 5                 {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},//10-90
 6                 {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},//100-900
 7                 {"","M","MM","MMM"}//1000-3000
 8         };
 9         StringBuilder sb = new StringBuilder();
10         int n=num/1000;
11         sb.append(array[3][n]);
12         num=num%1000;
13
14         n=num/100;
15         sb.append(array[2][n]);
16         num=num%100;
17
18         n=num/10;
19         sb.append(array[1][n]);
20         num=num%10;
21
22         sb.append(array[0][num]);
23
24         return sb.toString();
25     }
26 }
时间: 2024-12-04 23:30:39

【leetcode】12. Integer to Roman的相关文章

【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. 题解: 观察 1 到 10 :Ⅰ,Ⅱ,Ⅲ,Ⅳ(IIII),Ⅴ,Ⅵ,Ⅶ,Ⅷ,Ⅸ,Ⅹ,Ⅺ 难点在于出现字符不能连续超过三次,以及大数左边至多有一个小数.所以要分情况:1 - 3,4,5 - 8,9.将小数在大数左边的情况摘出来. Solution 1 贪心策略 1 class Solut

【leetcode】7 integer to roman

整数转换为罗马字符 注意事项: 1 将常用罗马字符保存咋二维数组中,供后期映射查询.存放规则:各位.十位等各一行 2 每次从数字的个位映射,循环直至为0 3 字符串result链接时注意顺序,与普通整数连接顺序不同 class Solution { public:     char*  roman[4][10] = { {"", "I", "II", "III", "IV", "V",

【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】Reverse Integer

题意: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路: 把整数倒转.很容易,只要先判断是否负数,存起来.之后取绝对值,把绝对值倒转后再决定是否是负数. 代码: class Solution { public: int reverse(int x) { bool neg = (x < 0); x = abs(x); int ans = 0; while(x

【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever

【leetcode】273. Integer to English Words

题目如下: Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hun

【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

【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 12. Integer to Roman | 字符串 | Medium

12. 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 int