網站首頁 編程語言 正文
羅馬數字轉整數
羅馬數字包含以下七種字符:?I
,?V
,?X
,?L
,C
,D
?和?M
。
字符 數值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000
例如, 羅馬數字 2 寫做?II
?,即為兩個并列的 1。12 寫做?XII
?,即為?X
?+?II
?。 27 寫做??XXVII
, 即為?XX
?+?V
?+?II
?。
通常情況下,羅馬數字中小的數字在大的數字的右邊。但也存在特例,例如 4 不寫做?IIII
,而是?IV
。數字 1 在數字 5 的左邊,所表示的數等于大數 5 減小數 1 得到的數值 4 。同樣地,數字 9 表示為?IX
。這個特殊的規則只適用于以下六種情況:
-
I
?可以放在?V
?(5) 和?X
?(10) 的左邊,來表示 4 和 9。 -
X
?可以放在?L
?(50) 和?C
?(100) 的左邊,來表示 40 和?90。? -
C
?可以放在?D
?(500) 和?M
?(1000) 的左邊,來表示?400 和?900。
給定一個羅馬數字,將其轉換成整數。輸入確保在 1?到 3999 的范圍內。
示例?1:
輸入:?"III" 輸出: 3
示例?2:
輸入:?"IV" 輸出: 4
示例?3:
輸入:?"IX" 輸出: 9
示例?4:
輸入:?"LVIII" 輸出: 58 解釋: L = 50, V= 5, III = 3.
示例?5:
輸入:?"MCMXCIV" 輸出: 1994 解釋: M = 1000, CM = 900, XC = 90, IV = 4.
筆者的方法:
時間200ms左右,
思路是
- 把所有的情況放到哈希表中
- 每次取一個位
- 把 i 和 i+1 放一起,試試有沒有區配的,有的話把 i 和 i+1 放一起
- 沒有的話,就是 只是計 i
public class Solution { public int RomanToInt(string s) { char[] c = s.ToCharArray(); //將其轉為字符數組 int sum = 0; //值 Hashtable hashtable = new Hashtable(); //7個基本單位 hashtable.Add("I", 1); hashtable.Add("V", 5); hashtable.Add("X", 10); hashtable.Add("L", 50); hashtable.Add("C", 100); hashtable.Add("D", 500); hashtable.Add("M", 1000); //加上6種情況 hashtable.Add("IV", 4); hashtable.Add("IX", 9); hashtable.Add("XL", 40); hashtable.Add("XC", 90); hashtable.Add("CD", 400); hashtable.Add("CM", 900);
/*
* 六種情況
IV 4 IX 9
XL 40 XC 90
CD 400 CM 9000
*/
for (int i = 0; i < c.Length; i++) { if (i + 1 < c.Length && hashtable.ContainsKey(c[i].ToString() + c[i + 1].ToString())) //如果發現兩位一起能區配的話 { sum += int.Parse(hashtable[c[i].ToString() + c[i + 1].ToString()].ToString()); //獲取值,HashTable的類型都是Object! i++; //跳兩位 } else { sum += int.Parse(hashtable[c[i].ToString()].ToString()); } } return sum; } }
?換成字典
public class Solution { public int RomanToInt(string s) { char[] c = s.ToCharArray(); //將其轉為字符數組 int sum = 0; //值 Dictionary<string, int> dictionary = new Dictionary<string, int>(); //7個基本單位 dictionary.Add("I", 1); dictionary.Add("V", 5); dictionary.Add("X", 10); dictionary.Add("L", 50); dictionary.Add("C", 100); dictionary.Add("D", 500); dictionary.Add("M", 1000); //加上6種情況 dictionary.Add("IV", 4); dictionary.Add("IX", 9); dictionary.Add("XL", 40); dictionary.Add("XC", 90); dictionary.Add("CD", 400); dictionary.Add("CM", 900);
/*
* 六種情況
IV 4 IX 9
XL 40 XC 90
CD 400 CM 9000
*/
for (int i = 0; i < c.Length; i++) { if (i + 1 < c.Length && dictionary.ContainsKey(c[i].ToString() + c[i + 1])) //如果發現兩位一起能區配的話 { sum += dictionary[c[i].ToString() + c[i + 1].ToString()]; //獲取值,HashTable的類型都是Object! i++; //跳兩位 } else { sum += dictionary[c[i].ToString()]; } } return sum; } }
?以上兩個例子都會進行較多的裝箱拆箱,下面主要使用if-else,switch,空間花銷較大,但是如果測試例子較多,進行大量計算,時間會相對少一點。
public class Solution { public int RomanToInt(string s) { int sum = 0; //值 for (int i = 0; i < s.Length; i++) { if (i + 1 < s.Length) //如果后面還有別的字符 { if (s[i] == 'I') { int a = 0; switch (s[i + 1]) //"i%" { case 'V': a = 4; i++; break; case 'X': a = 9; i++; break; default: a = 1; break; } sum += a; } else if (s[i] == 'X') { int a = 0; switch (s[i + 1]) //"X%" { case 'L': a = 40; i++; break; case 'C': a = 90; i++; break; default: a = 10; break; } sum += a; } else if (s[i] == 'C') { int a = 0; switch (s[i + 1]) //"X%" { case 'D': a = 400; i++; break; case 'M': a = 900; i++; break; default: a = 100; break; } sum += a; } else { int a = 0; switch (s[i]) {case 'V': a = 5; break;case 'L': a = 50; break;case 'D': a = 500; break; case 'M': a = 1000; break; } sum += a; } } else { int a = 0; switch (s[i]) { case 'I': a = 1; break; case 'V': a = 5; break; case 'X': a = 10; break; case 'L': a = 50; break; case 'C': a = 100; break; case 'D': a = 500; break; case 'M': a = 1000; break; } sum += a; } } return sum; } }
原文鏈接:https://www.cnblogs.com/whuanle/p/10352752.html
相關推薦
- 2022-06-01 python中使用正則表達式的方法詳解_python
- 2022-07-20 React使用有限狀態機的實現示例_React
- 2022-06-04 CZGL.ProcessMetrics監控.NET應用_實用技巧
- 2022-09-20 Android畫圖實現MPAndroidchart折線圖示例詳解_Android
- 2022-05-02 Entity?Framework中執行sql語句_實用技巧
- 2023-01-31 C#實現批量壓縮和解壓縮的示例代碼_C#教程
- 2022-06-27 ABP引入Dapper框架的創建使用_實用技巧
- 2022-07-06 C#中屬性(Attribute)的用法_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支