【leetcode】Roman to Integer & Integer to Roman

Given a roman numeral, convert it to an integer.

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

首先,学习一下罗马数字,参考罗马数字 
罗马数字是最古老的数字表示方式,比阿拉伯数组早2000多年,起源于罗马 
罗马数字有如下符号:

基本字符 I V X L C D M
对应阿拉伯数字 1 5 10 50 100 500 1000

计数规则: 相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3小的数字在大的数字右边,所表示的数等于这些数字相加得到的数,例如:VIII = 8小的数字,限于(I、X和C)在大的数字左边,所表示的数等于大数减去小数所得的数,例如:IV = 4正常使用时,连续的数字重复不得超过三次在一个数的上面画横线,表示这个数扩大1000倍(本题只考虑3999以内的数,所以用不到这条规则) 
其次,罗马数字转阿拉伯数字规则(仅限于3999以内):

从前向后遍历罗马数字,如果某个数比前一个数小,则加上该数。反之,减去前一个数的两倍然后加上该数

 1 class Solution {
 2 public:
 3     int romanToInt(string s) {
 4
 5         int result=0;
 6         map<char,int> roman;
 7         roman[‘I‘]=1;
 8         roman[‘V‘]=5;
 9         roman[‘X‘]=10;
10         roman[‘L‘]=50;
11         roman[‘C‘]=100;
12         roman[‘D‘]=500;
13         roman[‘M‘]=1000;
14
15         for(int i=s.length()-1;i>=0;i--)
16         {
17             if(i==s.length()-1)
18             {
19                 result=roman[s[i]];
20                 continue;
21             }
22             if(roman[s[i]]>=roman[s[i+1]])
23                 result+=roman[s[i]];
24             else
25                 result-=roman[s[i]];
26         }
27
28         return result;
29     }
30 };

Integer to Roman:

 1 class Solution {
 2 public:
 3     string intToRoman(int num) {
 4         string str;
 5         string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
 6         int value[]=    {1000,900,500,400, 100, 90,  50, 40,  10, 9,   5,  4,   1};
 7         for(int i=0;num!=0;++i)
 8         {
 9             while(num>=value[i])
10             {
11                 num-=value[i];
12                 str+=symbol[i];
13             }
14         }
15         return str;
16     }
17 };
时间: 2024-08-08 13:59:28

【leetcode】Roman to Integer & Integer to Roman的相关文章

【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer 题意:完成内置函数atoi的功能,将字符串转换成整数. 教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理.然后按照C++ Reference中关于atoi的规定一条一条写,才AC.另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以

【LeetCode】8. String to Integer (atoi) 字符串转整数

题目: Implement atoi to convert a string to an 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

【LeetCode】8 - String to Integer (atoi)

Implement atoi to convert a string to an 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 spe

【leetcode】1394. Find Lucky Integer in an Array

题目如下: Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value. Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky inte

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

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【LeetCode】First Missing Positive 解题报告

[题目] Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. [解析] 题意:给定一个数组,找出第一个缺失的正数,要求时间复杂度为O(n),空间复杂度为