请将给出的罗马数字转化为整数
保证输入的数字范围在1 到 3999之间。
public int romanToInt(String s) { int nums[]=new int[s.length()]; for(int i=0;i<s.length();i++){ switch (s.charAt(i)){ case 'M': nums[i]=1000; break; case 'D': nums[i]=500; break; case 'C': nums[i]=100; break; case 'L': nums[i]=50; break; case 'X' : nums[i]=10; break; case 'V': nums[i]=5; break; case 'I': nums[i]=1; break; } } int sum=0; for(int i=0;i<nums.length-1;i++){ if(nums[i]<nums[i+1]) sum-=nums[i]; else sum+=nums[i]; } return sum+nums[nums.length-1]; }
/*
* 基本字符 对应的数字
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;
小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12;
小的数字、(限于 Ⅰ、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;
正常使用时、连写的数字重复不得超过三次;
在一个数的上面画一条横线、表示这个数扩大 1000 倍。
*/
import java.util.*;
public class Solution {
public int romanToInt(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int ans = 0;
int preValue = 0;
for (int i = s.length() - 1; i >= 0; i--) {
int curValue = map.get(s.charAt(i));
if (curValue < preValue)
ans -= curValue;
else
ans += curValue;
preValue = curValue;
}
return ans;
}
}
class Solution {
public:
int romanToInt(string s) {
map<char,int> Map;
Map['I'] = 1;
Map['V'] = 5;
Map['X'] = 10;
Map['L'] = 50;
Map['C'] = 100;
Map['D'] = 500;
Map['M'] = 1000;
int sum = 0;
for(int i = 0; i < s.length(); i++)
{
if(Map[s[i]] < Map[s[i+1]] )
{
sum += Map[s[i+1]] - Map[s[i]];
i++;
}
else
sum += Map[s[i]];
}
return sum;
}
};
int romanToInt(string s) {
if(s.size() < 1) return 0;
map<char, int> mp = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
int sum = mp[s.back()];
for (int i = s.size() - 2; i >= 0; --i) {
//当前比之后的小则减去,逆序遍历之后的已经加过
if(mp[s[i]] < mp[s[i+1]])
sum -= mp[s[i]];
else
sum += mp[s[i]];
}
Leetcode#13. Roman to Integer(罗马数字转整数)
import java.util.HashMap;
/**
* 13\. Roman to Integer(罗马数字转整数)
* 给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
*/
public class Solution {
public static void main(String[] args) {
Solution13 solution13 = new Solution13();
String s = "IV";
System.out.println(solution13.romanToInt(s));
}
/**
* 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
* 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
* 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9。
* 利用 map 来完成罗马数字的 7 个数字符号:I、V、X、L、C、D、M 和整数的映射关系,然后根据上面的解释来模拟完成即可。
*
* @param s
* [@return](/profile/547241) */
public int romanToInt(String s) {
HashMap map = new HashMap();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int len = s.length();
int res = map.get(s.charAt(len - 1));
for (int i = len - 2; i >= 0; i--) {
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
res -= map.get(s.charAt(i));
} else {
res += map.get(s.charAt(i));
}
}
return res;
}
}
public class Solution {
public int romanToInt(String s) {
/**
* I 1
V 5
X 10
L 50
C 100
D 500
M 1000
*/
int[] nums = new int[s.length()];
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
switch (chars[i]){
case 'M':
nums[i] = 1000;
break;
case 'D':
nums[i] = 500;
break;
case 'C':
nums[i] = 100;
break;
case 'L':
nums[i] = 50;
break;
case 'X':
nums[i] = 10;
break;
case 'V':
nums[i] = 5;
break;
case 'I':
nums[i] = 1;
break;
}
}
int result = 0;
for (int i = 0; i < nums.length-1; i++) {
if (nums[i]<nums[i+1]){
result -= nums[i];
}else {
result += nums[i];
}
}
result += nums[nums.length-1];
return result;
}
public static void main(String[] args){
Solution solution = new Solution();
int result = solution.romanToInt("MCMXCIV");
System.out.println(result);
}
}
int res = 0;
unordered_map<char, int> dict{ { 'I',1}, {'V', 5},{'X', 10}, {'L', 50}, {'C', 100}, {'D',500},{'M', 1000} };
for (int i = 0; i < s.size(); ++i)
{
auto it = dict.find(s[i]);
if (i < s.size() - 1 && it->second < dict.find(s[i + 1])->second)
res -= it->second;
else
res += it->second;
}
return res;
class Solution {
public:
int romanToInt(string s) {
int result = 0; for(int i=0;i<s.length();i++) { if(i>0 && (T(s[i])>T(s[i-1]))) result += (T(s[i]) - 2*T(s[i-1])); else result += T(s[i]); } return result;
}
int T(char c)
{
switch(c)
{
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0; } }
};
class Solution {
public:
int romanToInt(string s) {
map<char,int> book;
book['I']=1;
book['V']=5;
book['X']=10;
book['L']=50;
book['C']=100;
book['D']=500;
book['M']=1000;
int i,sum=0;
for(i=0;i<s.length();i++)
if(i>0&&book[s[i]]-book[s[i-1]]>0) sum+=book[s[i]]-2*book[s[i-1]];
else sum+=book[s[i]];
return sum;
}
};
/*给罗马数字的string 然后求integer
罗马字符可能是单独是一个字母表示 或者两个在一起是一个数字
如果当前比前一个大,说明这一段的值应该是当前这个值减去上一个值。比如IV = 5 – 1
否则,将当前值加入到结果中,然后开始下一段记录。比如VI = 5 + 1, II=1+1
*/
public static int romanToInt(String str) {
int res = 0;
char[] array = str.toCharArray();
for(int i = 0;i<array.length;i++){
if(i>0 && getChar(array[i])>getChar(array[i-1])){
//如果后一位比前一位大这一段的值应该是当前这个值减去上一个值。比如IV = 5 – 1
res = res + (getChar(array[i])-2*getChar(array[i-1]));
}else{
res = res + getChar(array[i]);
}
}
return (int)res;
}
private static int getChar(char c) {
switch (c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
}
return 0;
}
import java.util.*;
public class Solution {
/**
*
* @param s string字符串
* @return int整型
*/
public int romanToInt (String s) {
// write code here
Map m = new HashMap();
m.put('I', 1);
m.put('V', 5);
m.put('X', 10);
m.put('L', 50);
m.put('C', 100);
m.put('D', 500);
m.put('M', 1000);
char[] ch = s.toCharArray();
int res = 0;
for (int i = 0; i < ch.length; i++) {
int num = m.get(ch[i]);
if (i > 0 && num > m.get(ch[i - 1]))
num -= 2*m.get(ch[i - 1]);
res += num;
}
return res;
}
}
class Solution:
def romanToInt(self , s ):
# 生成一个字典,键为单个罗马字母,值为一个列表,列表的第一个值为等级,第二个值为相应的十进制数。
d = {'I':[0,1],'V':[1,5],'X':[2,10],'L':[3,50],'C':[4,100],'D':[5,500],'M':[6,1000]}
integer = 0 # 存放输出值
L = list(s)
p_r=0 # 右指示等级,初始为0级
for i in reversed(L): # 反向迭代
p_l = d[i][0] # 左指示等级,表示当前阅读的罗马字母的等级
if p_l>=p_r: # 当前字母的等级比右边高,则直接相加
integer+=d[i][1]
else: # 否则减去该字母对应的十进制数
integer-=d[i][1]
p_r = d[i][0] # 向左移动右指示物,
return integer public class Solution {
public static void main(String[] args) {
System.out.println(new Solution().romanToInt("DCXXI"));
}
/**
* @param s string字符串
* @return int整型
*/
public int romanToInt(String s) {
// write code here
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] strs = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int temp = 0;
StringBuilder s1 = new StringBuilder(s);
for (int i = 0; i < values.length; i++) {
while (s1.indexOf(strs[i]) == 0) {
temp += values[i];
s1.delete(0, strs[i].length());
}
}
return temp;
}
} import java.util.*;
public class Solution {
/**
*
* @param s string字符串
* @return int整型
*/
public int romanToInt (String s) {
// write code here
HashMap<Character,Integer> 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()-1;i++)
{
if(map.get(s.charAt(i))>=map.get(s.charAt(i+1)))
{
res += map.get(s.charAt(i));
}
else
res -= map.get(s.charAt(i));
}
res += map.get(s.charAt(s.length()-1));
return res;
}
} class Solution {
public:
int romanToInt(string s) {
map<char,int> book = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50},
{'C', 100}, {'D', 500}, {'M', 1000}};
int res=0;
for(int i=0;i<s.length();i++)
if(i > 0 && book[s[i]] > book[s[i-1]]) sum += book[s[i]] - 2 * book[s[i-1]];
else sum += book[s[i]];
return sum;
}
};
class Solution
{
public int romanToInt(String s)
{
int Int = 0;
for (int i = 0; i < s.length(); i++)
switch (s.charAt(i))
{
case 'I':
if (i == s.length() - 1)
Int++;
else if (s.charAt(i + 1) == 'V' || s.charAt(i + 1) == 'X')
Int--;
else
Int++;
break;
case 'V':
Int += 5;
break;
case 'X':
if (i == s.length() - 1)
Int += 10;
else if (s.charAt(i + 1) == 'L' || s.charAt(i + 1) == 'C')
Int -= 10;
else
Int += 10;
break;
case 'L':
Int += 50;
break;
case 'C':
if (i == s.length() - 1)
Int += 100;
else if (s.charAt(i + 1) == 'D' || s.charAt(i + 1) == 'M')
Int -= 100;
else
Int += 100;
break;
case 'D':
Int += 500;
break;
case 'M':
Int += 1000;
}
return Int;
}
}
class Solution {
public:
int romanToInt(string s) {
const int values[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
const string strs[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
const int widths[]={1,2,1,2,1,2,1,2,1,2,1,2,1};
int res=0;
int index=0;
while(index<s.size()){
for(int i=0;i<sizeof(strs)/sizeof(strs[0]);i++){
if((widths[i]==1 && s[index]==strs[i][0])
|| (widths[i]==2 && index<s.size()-1 && s[index]==strs[i][0] && s[index+1]==strs[i][1])){
res+=values[i];
index+=widths[i];
break;
}
}
}
return res;
}
};