Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

roman to int

public int RomanToInt(string s)
    {
        if (s == null || s == string.Empty)
            return 0;
        
        Dictionary<string, int> dict = new Dictionary<string, int>();
        int result = 0;
        
        dict.Add("I", 1);
        dict.Add("V", 5);
        dict.Add("X", 10);
        dict.Add("L", 50);
        dict.Add("C", 100);
        dict.Add("D", 500);
        dict.Add("M", 1000);
        dict.Add("IV", 4);
        dict.Add("IX", 9);
        dict.Add("XL", 40);
        dict.Add("XC", 90);
        dict.Add("CD", 400);
        dict.Add("CM", 900);
        
        for (int i = 0; i < s.Length; i++)
            if ((s[i] == 'I' || s[i] == 'X' || s[i] == 'C') && i < s.Length - 1 && dict.ContainsKey(s.Substring(i, 2)))
                result += dict[s.Substring(i++, 2)];
            else
                result += dict[s[i].ToString()];
        
        return result;   
    }
Comment

Roman to Integer

class Solution:
    def romanToInt(self, s):
 
        values = {'I': 1, 'V': 5, 'X': 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}   
        result = 0
        for i in range(len(s)):
            if i + 1 == len(s) or values[s[i]] >= values[s[i + 1]] : # if current item is not the last item on the string
                                                                    # or current item's value is greater than or equal to next item's value 
                result = result + values[s[i]]                      # then add current item's value from result
            else:
                result = result - values[s[i]]                      # otherwise subtract current item's value from result
        return result

Task = Solution()
print(Task.romanToInt("III"))
print(Task.romanToInt("LVIII"))
print(Task.romanToInt("MCMXCIV"))
Comment

Roman to integer

string[] s = { "I", "V", "X", "L", "C", "D", "M" };
int[] r = { 1, 5, 10, 50, 100, 500, 1000 };
string roman;
roman = Console.ReadLine();
int n = 0;
for (int i = 0; i < roman.Length; i++)
{
  for (int j = 0; j < s.Length; j++)
  {
    if (roman[i].ToString() == s[j])
    {
      for (int k = 0; k < s.Length; k++)
      {
        if (i + 1 < roman.Length)
        {
          if (roman[i + 1].ToString() == s[k])
          {
            if (k > j)
            {
              n -= r[j];
            }
            else
            {
              n += r[j];
            }
            break;
          }
        }
        else
        {
          n += r[j];
          break;
        }
      }
      break;
    }
  }
}
Console.WriteLine(n);
Comment

Roman to Integer

var romanToInt = function (s) {

  const object = {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
  }
  let count =0
  console.log(object)
 
 for(i =0; i<s.length;i++){
   const cur = object[s[i]]
   const next = object[s[i+1]]

 if(cur<next){
   count += next-cur
   i++
 }else{
   count+= cur
 }
}
return count

};
Comment

Roman to Integer

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000
Comment

Roman to Integer

# @param {String} s
# @return {Integer}
def roman_to_int(s)
    
end
Comment

Roman to Integer

class Solution {
    func romanToInt(_ s: String) -> Int {
        
    }
}
Comment

Roman to Integer



int romanToInt(char * s){

}
Comment

Roman to Integer

function romanToInt(s: string): number {

};
Comment

Roman to Integer

class Solution {

    /**
     * @param String $s
     * @return Integer
     */
    function romanToInt($s) {
        
    }
}
Comment

Roman to Integer

public class Solution {
    public int RomanToInt(string s) {
        
    }
}
Comment

Roman to Integer

/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
    
};
Comment

Roman to Integer

class Solution {
    public int romanToInt(String s) {
        
    }
}
Comment

Roman to Integer

class Solution {
public:
    int romanToInt(string s) {
        
    }
};
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# list object sort alphabetically 
Csharp :: c# space as string 
Csharp :: billboard canvas unity 
Csharp :: string to byte array c# 
Csharp :: c# getting user input 
Csharp :: c# remove first 5 characters from string 
Csharp :: wpf messagebox result 
Csharp :: c# sum of array elements# 
Csharp :: Unity Interstitial ad C# 
Csharp :: github action get commit tag 
Csharp :: unity find child by name 
Csharp :: The entity type has multiple properties with the [Key] attribute. 
Csharp :: unity no serializefield 
Csharp :: c# read csv file 
Csharp :: char to digit in java 
Csharp :: integer required asp.net core 
Csharp :: make command prompt hidden c# 
Csharp :: copy class c# 
Csharp :: asp.net core miniprofiler 
Csharp :: mongodb driver c# nuget 
Csharp :: c# check if char is string 
Csharp :: c# array max 
Csharp :: c# close form 
Csharp :: C# new form 
Csharp :: how to get file type from base64 in c# 
Csharp :: Hello World Dotnet 
Csharp :: draw on picturebox c# 
Csharp :: unity get pivot position 
Csharp :: unity2d movement 
Csharp :: c# Dictionary contains key case insensitive 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =