LeetCode之Easy篇 ——(12)Roman to Integer

Given an integer, convert it to a roman numeral.

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

罗马数字:

基本字符
I

V

X

L

C

D

M
相应的阿拉伯数字表示为
1

5

10

50

100

500

1000

1、相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;

2、小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12;

3、小的数字(限于 I、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;

4、正常使用时、连写的数字重复不得超过三次;

5、在一个数的上面画一条横线、表示这个数扩大 1000 倍。

思路:

  首先,用二维数组保存罗马数字的个位、整十、整百、整千(如1000、2000...其他类似)。举例:[0][1]代表“I”,[1][0]代表X,[1][1]代表XX。

  其次,将输入值的各位取出来,再输出对应数组里的罗马字符。举例:若取出来的是百位上的3,则对应数组的[2][2],即是它对应的300。

Java代码实现:

public class Solution {
    public String intToRoman(int num) {
        String[][] s = new String[][]{{"", "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"}};
        return s[3][num / 1000 % 10] + s[2][num / 100 % 10] + s[1][num / 10 % 10] + s[0][num % 10];
    }
}

原文地址:https://www.cnblogs.com/promiseslc/p/8598028.html

时间: 2024-08-12 22:37:24

LeetCode之Easy篇 ——(12)Roman to Integer的相关文章

LeetCode之Easy篇 ——(13)Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 思路分析: 1.熟悉罗马数字的规则.见LeetCode之Easy篇 --(12)Integer to Roman 2.将输入的罗马数字转化成数组,并逐一通过case比对,然后根据其规则进行运算. Java代码示例: class Solution { public int romanT

【leetcode刷题笔记】Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字:否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字.利用一个HashMap存放罗马字符和数字的对应. 罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.

LeetCode之Easy篇 ——(7)Reverse Integer

7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note:Assume we are dealing with an environment which could only hold

leetcode修炼之路——13. Roman to Integer

题目: Given a roman numeral, convert it to an integer. 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). 按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍.右加左减:在一个较大的罗马数

【LeetCode从零单刷】Roman to Integer

题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 解答: 说实话这题目就是欺负中国人不了解罗马数字.可以参考维基百科中相关介绍:传送门.其中有性质如下: 羅馬數字共有7個,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 右加左減: 在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數

[LeetCode][JavaScript]Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. https://leetcode.com/problems/roman-to-integer/ 罗马数字转阿拉伯数字. 从后往前扫,如果当前的数大于之前的数,加上这个数,反之减去当前的数. 1 /** 2 * @param {string} s 3

Roman to Integer [LeetCode 13]

1- 问题描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 2- 罗马数字[1] 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗马数字中没有“0”,与进位制无关.一般认为罗马数字只用来记数,而不作演算. 重复数次:一个

[LeetCode OJ] Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 下面是百度得到的关于罗马数的解释: 我的代码: 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 map<char,int> conversion; 5 conversion.insert(make_pair('

Leetcode 13. Roman to Integer(水)

13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve i