Force buckle Net - Roman numeral to integer

Posted by tastyniall on Fri, 11 Feb 2022 19:48:22 +0100

Problem introduction

  this problem is actually related to Integer to Roman The numbers are equivalent.

Programming ideas

Write some complete conversion functions (including 400, 900, 40, 90, etc.)

Map<String, Integer> transfer = new HashMap<>();
        transfer.put("I", 1);
        transfer.put("IV", 4);
        transfer.put("V", 5);
        transfer.put("IX", 9);
        transfer.put("X", 10);
        transfer.put("XL", 40);
        transfer.put("L", 50);
        transfer.put("XC", 90);
        transfer.put("C", 100);
        transfer.put("CD", 400);
        transfer.put("D", 500);
        transfer.put("CM", 900);
        transfer.put("M", 1000);

The conversion function does not include 400, 900, etc

  the problem is very simple when you understand it. First, create a HashMap to map symbols and values, and then the string from left to right. If the value represented by the current character is not less than its right, add the value; Otherwise, subtract the value. By analogy to the leftmost number, the final result is the answer

Map<String, Integer> transfer = new HashMap<>();
        transfer.put("I", 1);
        transfer.put("V", 5);
        transfer.put("X", 10);
        transfer.put("L", 50);
        transfer.put("C", 100);
        transfer.put("D", 500);
        transfer.put("M", 1000);

code implementation

AC first solution

    public int romanToInt(String s) {
        Map<String, Integer> transfer = new HashMap<>();
        transfer.put("I", 1);
        transfer.put("IV", 4);
        transfer.put("V", 5);
        transfer.put("IX", 9);
        transfer.put("X", 10);
        transfer.put("XL", 40);
        transfer.put("L", 50);
        transfer.put("XC", 90);
        transfer.put("C", 100);
        transfer.put("CD", 400);
        transfer.put("D", 500);
        transfer.put("CM", 900);
        transfer.put("M", 1000);
        int result = 0;
        for (int i = 0; i < s.length(); ) {
            String s1 = s.substring(i, i + 1);
            String s2 = "";
            if (i == s.length() - 1) {
                s2 = s1;
            } else {
                s2 = s.substring(i, i + 2);
            }

            if (transfer.containsKey(s2)) {
                result += transfer.get(s2);
                i += 2;
            } else {
                result += transfer.get(s1);
                i++;
            }
        }
        return result;
    }

The second AC solution

III 3
IV 4
V 5
IX 9
LVIII 58
MCMXCIV 1994

First version - problem

    public int romanToInt(String s) {
        Map<String, Integer> transfer = new HashMap<>();
        transfer.put("I", 1);
        transfer.put("IV", 4);
        transfer.put("V", 5);
        transfer.put("IX", 9);
        transfer.put("X", 10);
        transfer.put("XL", 40);
        transfer.put("L", 50);
        transfer.put("XC", 90);
        transfer.put("C", 100);
        transfer.put("CD", 400);
        transfer.put("D", 500);
        transfer.put("CM", 900);
        transfer.put("M", 1000);
        int result = 0;
        for (int i = 0; i < s.length(); i += 2) {
            String s1 = s.substring(i, i + 1);
            String s2;
            if (i < s.length() - 1) {
                s2 = s.substring(i + 1, i + 2);
            } else {
                s2 = s1;
            }

            Integer left = transfer.get(s1);
            Integer right = transfer.get(s2);
            if (left > right) {
                result += left;
                result += right;

            } else if (left < right) {
                result += right;
                result -= left;
            } else {
                // left = right
                // In this position, if the semantics is not easy to understand, the code that is not easy to understand is not good code
                if (i < s.length() - 1) {
                    result += left;
                    result += right;
                } else {
                    result += left;
                }

            }


        }
        return result;

    }

  there is a problem with this program, because for MCMXCIV, although m > C, it can not directly deal with two. At present, it is a problem to treat left and right as equivalent.

AC version

  it must be understood that in the process of processing, if the number corresponding to the first character is less than the number of the second character, the semantics is determined, but if the number corresponding to the first character > = the number corresponding to the second character, the number on the left can be determined.

You can use examples

MCMXCIV 1994

Although the number 1000 corresponding to M is greater than the number 100 corresponding to C, C cannot be used as an independent whole, because it is uncertain whether C is 100 or there is M after C. at this time, CM exists as a whole.

    public int romanToInt(String s) {
        Map<String, Integer> transfer = new HashMap<>();
        transfer.put("I", 1);
        transfer.put("V", 5);
        transfer.put("X", 10);
        transfer.put("L", 50);
        transfer.put("C", 100);
        transfer.put("D", 500);
        transfer.put("M", 1000);
        int result = 0;
        for (int i = 0; i < s.length();) {
            String s1 = s.substring(i, i + 1);
            String s2;
            if (i < s.length() - 1) {
                s2 = s.substring(i + 1, i + 2);
            } else {
                // s2 = s1 is also used cleverly. Because the following transfer Get (S2) will not throw an exception.
                s2 = s1;
            }

            Integer left = transfer.get(s1);
            Integer right = transfer.get(s2);
            if (left >= right) {
                result += left;
                i++;
            } else if (left < right) {
                result += right;
                result -= left;
                i += 2;
            }

        }
        return result;

    }

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG ynrndmhp-1644409854520)( https://s2.loli.net/2022/02/09/VQXZEkhNPYImOue.png )]

summary

  two things happened today. One is that my father is drunk. I'm very worried at night. There's an idiom called self infliction. I can't bear it for him. It's very uncomfortable. Sleeping with him at night is full of drunkenness. I must not get drunk in the future. No matter what occasion, I can drink as much as I can. Don't worry your family. In addition, I stayed up late. I didn't go to bed until 1 o'clock last night. I felt very bad and uncomfortable the next day. When I work this year, I strive to go to bed at 10 o'clock every day. Nourish the spirit, consolidate the foundation and cultivate the yuan.

  family is really not a place to quarrel, but a place to show love. I hope readers can play the role of good love in their own home. Don't be angry, let alone complain about others. People with resentment can't get happiness and a happy smile, which will be very unfortunate.

  when I was programming, I suddenly thought of using it in typera. When writing code, I added code blocks. By default, I added java code blocks. Finally, I read the article ahk implementation Yes.