成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Integer to Roman & Roman to Integer

pkwenda / 789人閱讀

摘要:建立映射整數(shù)數(shù)組字符串?dāng)?shù)組,這兩個(gè)數(shù)組都要從大到小,為了方便之后對(duì)整數(shù)進(jìn)行從大到小的分解,以便用從前向后建立數(shù)字。建立,存入的數(shù)值對(duì)應(yīng)關(guān)系。

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:

建立映射:整數(shù)數(shù)組num -- 字符串?dāng)?shù)組roman,這兩個(gè)數(shù)組都要從大到小,為了方便之后對(duì)整數(shù)n進(jìn)行從大到小的分解,以便用StringBuilder()從前向后建立Roman數(shù)字。

Roman to Integer:

建立HashMap,存入Roman的數(shù)值對(duì)應(yīng)關(guān)系。然后從String s從前向后遍歷每個(gè)字符,找到map對(duì)應(yīng)的值累加,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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65513.html

相關(guān)文章

  • [Leetcode] Roman to Integer and Integer to Roman

    摘要:正則表達(dá)式思路首先我們要熟悉羅馬數(shù)的表達(dá)方式。驗(yàn)證字符串是否是羅馬數(shù),我們先看一下有效的羅馬數(shù)是什么樣的,假設(shè)該數(shù)字小于,從千位到個(gè)位依次拆解。 Valid Roman Numeral 正則表達(dá)式 思路 首先我們要熟悉羅馬數(shù)的表達(dá)方式。M是1000,D是500,C是100,L是50,X是10,V是5,I是1。驗(yàn)證字符串是否是羅馬數(shù),我們先看一下有效的羅馬數(shù)是什么樣的,假設(shè)該數(shù)字小于50...

    wdzgege 評(píng)論0 收藏0
  • leetcode 12 Integer to Roman

    摘要:題目詳情題目的意思是輸入一個(gè)阿拉伯?dāng)?shù)字,我們需要輸出這個(gè)數(shù)字的羅馬數(shù)字表示形式字符串。想法這道題最重要的點(diǎn)就是理解羅馬數(shù)和阿拉伯?dāng)?shù)之間的轉(zhuǎn)換規(guī)律。 題目詳情 Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.題目的意思是: 輸...

    wqj97 評(píng)論0 收藏0
  • Leetcode12 Integer to Roman

    摘要:解題思路其中每?jī)蓚€(gè)階段的之間有一個(gè)減法的表示,比如,寫在前面表示。所以映射關(guān)系應(yīng)該是然后就是貪心的做法,每次選擇能表示的最大值,把對(duì)應(yīng)的字符串連起來。 Roman to Integer Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fro...

    CoorChice 評(píng)論0 收藏0
  • Leetcode PHP題解--D82 13. Roman to Integer

    摘要:題目鏈接題目分析將給定的羅馬數(shù)字轉(zhuǎn)換成阿拉伯?dāng)?shù)字。要注意,先替換連續(xù)出現(xiàn)的那些。最終代碼若覺得本文章對(duì)你有用,歡迎用愛發(fā)電資助。 D82 13. Roman to Integer 題目鏈接 13. Roman to Integer 題目分析 將給定的羅馬數(shù)字轉(zhuǎn)換成阿拉伯?dāng)?shù)字。 思路 用替換法。 要注意,先替換連續(xù)出現(xiàn)的那些。例如,比先替換I,要先替換III。 最終代碼

    CODING 評(píng)論0 收藏0
  • 【LeetCode Easy】013 Roman to Integer

    摘要:將羅馬字母的字符串轉(zhuǎn)換為代表的整數(shù)這題不難,用一個(gè)存羅馬數(shù)字和具體數(shù)字的對(duì)應(yīng)關(guān)系,然后遍歷前后兩兩比較,該加加,該減減時(shí)間復(fù)雜度這里是自己寫的一個(gè)方法,里面用一個(gè),相當(dāng)于存對(duì)應(yīng)當(dāng)時(shí)一直想著用一個(gè)來存減的值,所以沒法用就用了指針,但其實(shí)就 Easy 013 Roman to Integer Description: 將羅馬字母的字符串轉(zhuǎn)換為代表的整數(shù)Roman numerals are ...

    wizChen 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<