1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)13: 羅馬數(shù)字轉(zhuǎn)整數(shù)

        共 1708字,需瀏覽 4分鐘

         ·

        2020-08-17 19:54

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !


        今天和大家聊的問題叫做羅馬數(shù)字轉(zhuǎn)整數(shù)?,我們先來看題面:

        https://leetcode-cn.com/problems/roman-to-integer/


        題意

        樣例


        示例?1:

        輸入:?"III"
        輸出:?3
        示例?2:

        輸入:?"IV"
        輸出:?4
        示例?3:

        輸入:?"IX"
        輸出:?9
        示例?4:

        輸入:?"LVIII"
        輸出:?58
        解釋: L =?50, V=?5, III =?3.

        題解


        哈希解法:用map建立起來鍵值對,每個可能組成的羅馬數(shù)字一 一對應為整數(shù),
        利用子字符串方法subStrirng拿出兩位字符串,然后在map中查找是否有這個key,如果有則拿出key鍵所對應的value值,用一個變量記錄為整數(shù)的值加這個value值,如果存在兩位的羅馬數(shù)字,則角標加2,如果不存在兩位羅馬數(shù)字,即是加1.


        public?static?int?romanToInt(String s)?{
        ????????HashMap map?= new?HashMap<>();
        ????????int?result = 0;
        ????????map.put("I", 1);
        ????????map.put("IV", 4);
        ????????map.put("V", 5);
        ????????map.put("IX", 9);
        ????????map.put("X", 10);
        ????????map.put("XL", 40);
        ????????map.put("L", 50);
        ????????map.put("XC", 90);
        ????????map.put("C", 100);
        ????????map.put("CD", 400);
        ????????map.put("D", 500);
        ????????map.put("CM", 900);
        ????????map.put("M", 1000);

        ????????for?(int?i = 0; i < s.length(); ) {
        ????????????if?(i + 1?< s.length() && map.containsKey(s.substring(i, i + 2))) {
        ????????????????result += map.get(s.substring(i, i + 2));
        ????????????????i += 2;
        ????????????} else?{
        ????????????????result += map.get(""?+ s.charAt(i));
        ????????????????i++;
        ????????????}
        ????????}
        ????????return?result;
        ????}


        這題官方給出的難度等級為簡單,大家應該都做出來了吧,當然本題還有其他解法,這里就不多說了,有興趣的LeetCode官網(wǎng)上看答案 。

        今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。


        上期推文:

        LeetCode刷題實戰(zhàn)1:在數(shù)組上遍歷出花樣

        LeetCode刷題實戰(zhàn)2:用鏈表模擬加法

        LeetCode刷題實戰(zhàn)3:最長不重復子串

        LeetCode刷題實戰(zhàn)4:兩個正序數(shù)組的中位數(shù)

        LeetCode刷題實戰(zhàn)5:判斷回文子串

        LeetCode刷題實戰(zhàn)6:Z字形變換

        LeetCode刷題實戰(zhàn)7:整數(shù)反轉(zhuǎn)

        LeetCode刷題實戰(zhàn)8:字符串轉(zhuǎn)換整數(shù)

        LeetCode刷題實戰(zhàn)9:求解回文數(shù)

        LeetCode刷題實戰(zhàn)10:字符串正則匹配

        LeetCode刷題實戰(zhàn)11: 盛最多水的容器

        LeetCode刷題實戰(zhàn)12: 整數(shù)轉(zhuǎn)羅馬數(shù)字


        瀏覽 27
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            chinaxxxxhdvideos在线 | 老女人的性生活视频 | 肏逼视频免费看 | 国产免费一级A片 | 日日摸夜夜爽视频 | 一级欧美大片 | 久久国产午夜 | 大香蕉伊网 | 91麻豆成人 | 女生抠逼视频 |