13. Roman to Integer

13. Roman to Integer


class Solution {
    public int romanToInt(String s) {
        if(s == null || s.length() == 0) return 0;
        int result = 0;
        // becauase later when we see the CM, so treat it as MC
        // later when we see CD. we treat it as DC
        // later when we see XC. we treat it as CX, 
        // later when we see XL. we treat it as LX, XL = 90, LX = 110, the diff is 20
        // later when we see IX. we treat it as XI, IX is 9, XI is 11, the diff is 2  
        // later when we see IV. we treat it as VI, VI = 6 , IV = 4, the diff is 2 
        // if we can find those substring at the string s, then we know later 
        // we are gonna add them , but with the opposite order, for example
        // we are gonna add the value of VI, instead of the value of IV. so
        // we substract the difference beforehand 
        if(s.indexOf("CM") != -1) result = result - 200;
        if(s.indexOf("CD") != -1) result = result - 200;
        if(s.indexOf("XC") != -1) result = result - 20;
        if(s.indexOf("XL") != -1) result = result - 20;
        if(s.indexOf("IX") != -1) result = result - 2;
        if(s.indexOf("IV") != -1) result = result - 2;
        
        for(char c : s.toCharArray()){
            if(c == 'M') result += 1000;
            else if(c == 'D') result += 500;
            else if(c == 'C') result += 100;
            else if(c == 'L') result += 50;
            else if(c == 'X') result += 10;
            else if(c == 'V') result += 5;
            else if(c == 'I') result += 1;
        }
        return result;
    
    }
}

 

The indexOf(String target) method searches left-to-right inside the given string for a "target" string. The indexOf() method returns the index number where the target string is first found or -1 if the target is not found. Like equals(), the indexOf() method is case-sensitive, so uppercase and lowercase chars are considered to be different.

So basically, if you were going to write a for-loop to iterate over a string and look for a string, indexOf() can just do it for you. Note that indexOf() works best if you want to find the firstinstance of the target. If you want to find all the instances, see the next section.

String str = "Here there everywhere";

int a = str.indexOf("there");  // a is 5
int b = str.indexOf("er");     // b is 1
int c = str.indexOf("eR");     // c is -1, "eR" is not found

 

 

 

 

 

Roman numerals are represented by seven different symbols: IVXLCD 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 is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

posted on 2018-08-28 20:39  猪猪🐷  阅读(106)  评论(0)    收藏  举报

导航