【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 Solution {
 2 public:
 3     string intToRoman(int num) {
 4         string res = "";
 5         vector<int> weights{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
 6         vector<string> tokens{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
 7
 8         int i = 0;
 9         while (num && i < weights.size()) {
10             while (num >= weights[i]) {
11                 num -= weights[i];
12                 res += tokens[i];
13             }
14             ++i;
15         }
16
17         return res;
18     }
19 };

Solution 2

 1 class Solution {
 2 public:
 3     string intToRoman(int num) {
 4         vector<string> M = { "", "M", "MM", "MMM" };
 5         vector<string> C = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
 6         vector<string> X = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
 7         vector<string> I = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
 8         return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
 9     }
10 };

原文地址:https://www.cnblogs.com/Atanisi/p/8645420.html

时间: 2024-11-01 18:58:04

【LeetCode】012. Integer to Roman的相关文章

【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 St

【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 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 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成

【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