大家好,我是毛毛。ヾ(´∀ ˋ)ノ
廢話不多說開始今天的解題Day~
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
For example, 2
is written as II
in Roman numeral, just two one's added together. 12
is written as XII
, which is simply X + II
. The number 27
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: s = "III"
Output: 3
Input: s = "IV"
Output: 4
Input: s = "IX"
Output: 9
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
1 <= s.length <= 15
s
contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M'
).s
is a valid roman numeral in the range [1, 3999]
.首先先簡單的翻譯一下題目
給你羅馬數字要轉回成十進位的數字表示,然後I
可以放在V
跟X
的前面,X
可以放在L
跟C
的前面,而C
可以放在D
跟M
的前面。
像是:IV,V代表的是5,I代表的是1,IV則代表的是4 (5-1),以此類推。
作法大致上是這樣
sum
。class Solution:
def romanToInt(self, s: str) -> int:
Symbol_dict = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
sum = 0
list_s = list(s)
for index in range(len(list_s)-1, -1, -1):
if index == (len(list_s)-1):
sum += Symbol_dict[list_s[index]]
else:
if Symbol_dict[list_s[index+1]] > Symbol_dict[list_s[index]]:
sum -= Symbol_dict[list_s[index]]
else:
sum += Symbol_dict[list_s[index]]
return sum
int romanToInt(char * s){
int Symbol_dict[] = {1, 5, 10, 50, 100, 500, 1000};
int sum = 0;
int Symbol_dict_index1 = -1, Symbol_dict_index2 = -1;
for(int index=strlen(s)-1; index>=0 ; index--) {
printf("%c\n", s[index]);
if (s[index] == 'I'){
Symbol_dict_index1 = 0;
} else if (s[index] == 'V'){
Symbol_dict_index1 = 1;
} else if (s[index] == 'X'){
Symbol_dict_index1 = 2;
} else if (s[index] == 'L'){
Symbol_dict_index1 = 3;
} else if (s[index] == 'C'){
Symbol_dict_index1 = 4;
} else if (s[index] == 'D'){
Symbol_dict_index1 = 5;
} else if (s[index] == 'M'){
Symbol_dict_index1 = 6;
}
if (index == (strlen(s)-1)){
sum += Symbol_dict[Symbol_dict_index1];
} else {
if (s[index+1] == 'I'){
Symbol_dict_index2 = 0;
} else if (s[index+1] == 'V'){
Symbol_dict_index2 = 1;
} else if (s[index+1] == 'X'){
Symbol_dict_index2 = 2;
} else if (s[index+1] == 'L'){
Symbol_dict_index2 = 3;
} else if (s[index+1] == 'C'){
Symbol_dict_index2 = 4;
} else if (s[index+1] == 'D'){
Symbol_dict_index2 = 5;
} else if (s[index+1] == 'M'){
Symbol_dict_index2 = 6;
}
if (Symbol_dict[Symbol_dict_index2] > Symbol_dict[Symbol_dict_index1]){
sum -= Symbol_dict[Symbol_dict_index1];
} else {
sum += Symbol_dict[Symbol_dict_index1];
}
}
}
return sum;
}
Python
C
大家明天見
感謝大大分享,我的做法是把羅馬數字6個減法實例直接寫成dictionary參照,效能跑分還可以,也分享給邦友另一種作法
https://www.youtube.com/watch?v=qC2tAhe133Y