13. Roman to Integer--LeetCode Record
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
let record:[Character:Int] = ["M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1]
func romanToInt(s: String) -> Int {
var index = s.endIndex.predecessor()
var result:Int = 0
var preCh = s[index]
result += record[preCh]!
if s.characters.count > 1 {
while index.predecessor() != s.startIndex {
index = index.predecessor()
result += middleResult(s[index], preCh)
preCh = s[index]
}
result += middleResult(s[s.startIndex], preCh)
}
return result
}
func middleResult(p: Character, _ q: Character)->Int {
if record[p]! < record[q]! {
return record[p]! * -1
}
return record[p]!
}
相關文章
- [Swift LeetCode]13. Roman to IntegerSwiftLeetCode
- LeetCode 13. Roman to Integer C語言LeetCodeC語言
- Leetcode Integer to RomanLeetCode
- leetcode Roman to IntegerLeetCode
- [LeetCode] Roman to IntegerLeetCode
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- Leetcode-Roman to IntegerLeetCode
- Leetcode-Integer to RomanLeetCode
- Integer to Roman leetcode javaLeetCodeJava
- Roman to Integer leetcode javaLeetCodeJava
- Leetcode 12. Integer to RomanLeetCode
- react-recordReact
- 13. 分組資料
- Times New Roman字型的歷史
- Daily record-SeptemberAI
- 實戰 Java 16 值型別 Record - 2. Record 的基本用法Java型別
- pl/sql record 詳解SQL
- delete duplication record in sql serverdeleteSQLServer
- 【深基13.例1】查詢
- Bootstrap系列 -- 13. 內聯表單boot
- 13.管理許可權(筆記)筆記
- LeetCode Roman to Integer(013)解法總結LeetCode
- 程式碼審查清單:Java併發 - Roman LeventovJava
- LeetCode - 解題筆記 - 12 - Integer to RomanLeetCode筆記
- Java 16 新特性:record類Java
- Erlang中的Record詳解
- 淺析 record 使用場景
- Record It for Mac錄屏軟體Mac
- Java 21 新特性:Record PatternsJava
- 脫離rails 使用Active RecordAI
- Homework record-Simple sorting
- gorm忽略報錯: record not foundGoORM
- LeetCode Integer to Roman(012)解法總結LeetCode
- Redis 實戰 —— 13. 擴充套件 RedisRedis套件
- [LeetCode] Integer to Roman 整數轉化成羅馬數字LeetCode
- [LeetCode] Roman to Integer 羅馬數字轉化成整數LeetCode
- 13. iOS開發小細節--OC篇iOS