【leetcode】7 integer to roman

整数转换为罗马字符

注意事项:

1 将常用罗马字符保存咋二维数组中,供后期映射查询。存放规则:各位、十位等各一行

2 每次从数字的个位映射,循环直至为0

3 字符串result链接时注意顺序,与普通整数连接顺序不同

class Solution {

public:     char*  roman[4][10] = {

{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},

{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},

{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},

{"", "M", "MM", "MMM","","","","","",""}             };

string intToRoman(int num) {

int rest=0;

int digit=0;

string result="";

while(num){

rest=num%10;

result=roman[digit][rest]+result;   //注意此处顺序,不能写result+=   roman[digit][rest];因为这是字符串处理,不是整数处理。

digit++;

num/=10;

}

return result;

} };

时间: 2024-08-29 21:00:05

【leetcode】7 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】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】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】N-queens

问题: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration