资讯专栏INFORMATION COLUMN

[LintCode] Integer to Roman & Roman to Integer

pkwenda / 584人阅读

摘要:建立映射整数数组字符串数组,这两个数组都要从大到小,为了方便之后对整数进行从大到小的分解,以便用从前向后建立数字。建立,存入的数值对应关系。

Problem

Integer to Roman
Given an integer, convert it to a roman numeral.
The number is guaranteed to be within the range from 1 to 3999.

Roman to Integer
Given a roman numeral, convert it to an integer.
The answer is guaranteed to be within the range from 1 to 3999.

Symbol   Value
I        1
V        5
X        10
L        50
C        100
D        500
M        1,000

Note

Integer to Roman:

建立映射:整数数组num -- 字符串数组roman,这两个数组都要从大到小,为了方便之后对整数n进行从大到小的分解,以便用StringBuilder()从前向后建立Roman数字。

Roman to Integer:

建立HashMap,存入Roman的数值对应关系。然后从String s从前向后遍历每个字符,找到map对应的值累加,if遇到前一位值小于后一位值的情况,减去前一位值的2倍(if外面多加了一次,减2倍减回来)。

Solution

Integer to Roman

public class Solution {
    public String intToRoman(int n) {
        // Write your code here
        String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int[] num = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (n != 0) {
            if (n >= num[i]) {
                sb.append(roman[i]);
                n -= num[i];
            }
            else i++;
        }
        return sb.toString();
    }
}

Roman to Integer

public class Solution {
    public int romanToInt(String s) {
        HashMap map = new HashMap();
        map.put("M", 1000);
        map.put("D", 500);
        map.put("C", 100);
        map.put("L", 50);
        map.put("X", 10);
        map.put("V", 5);
        map.put("I", 1);
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            res += map.get(s.charAt(i));
            if (i > 0 && map.get(s.charAt(i-1)) < map.get(s.charAt(i))) 
                res -= 2 * map.get(s.charAt(i-1));
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65513.html

相关文章

  • [Leetcode] Roman to Integer and Integer to Roman

    摘要:正则表达式思路首先我们要熟悉罗马数的表达方式。验证字符串是否是罗马数,我们先看一下有效的罗马数是什么样的,假设该数字小于,从千位到个位依次拆解。 Valid Roman Numeral 正则表达式 思路 首先我们要熟悉罗马数的表达方式。M是1000,D是500,C是100,L是50,X是10,V是5,I是1。验证字符串是否是罗马数,我们先看一下有效的罗马数是什么样的,假设该数字小于50...

    wdzgege 评论0 收藏0
  • 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.题目的意思是: 输...

    wqj97 评论0 收藏0
  • Leetcode12 Integer to Roman

    摘要:解题思路其中每两个阶段的之间有一个减法的表示,比如,写在前面表示。所以映射关系应该是然后就是贪心的做法,每次选择能表示的最大值,把对应的字符串连起来。 Roman to Integer Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fro...

    CoorChice 评论0 收藏0
  • Leetcode PHP题解--D82 13. Roman to Integer

    摘要:题目链接题目分析将给定的罗马数字转换成阿拉伯数字。要注意,先替换连续出现的那些。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D82 13. Roman to Integer 题目链接 13. Roman to Integer 题目分析 将给定的罗马数字转换成阿拉伯数字。 思路 用替换法。 要注意,先替换连续出现的那些。例如,比先替换I,要先替换III。 最终代码

    CODING 评论0 收藏0
  • 【LeetCode Easy】013 Roman to Integer

    摘要:将罗马字母的字符串转换为代表的整数这题不难,用一个存罗马数字和具体数字的对应关系,然后遍历前后两两比较,该加加,该减减时间复杂度这里是自己写的一个方法,里面用一个,相当于存对应当时一直想着用一个来存减的值,所以没法用就用了指针,但其实就 Easy 013 Roman to Integer Description: 将罗马字母的字符串转换为代表的整数Roman numerals are ...

    wizChen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<