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]!
}
相關文章
- 13. Roman to Integer
- LeetCode 13. Roman to Integer C語言LeetCodeC語言
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- Leetcode 12. Integer to RomanLeetCode
- react-recordReact
- Active Record Associations
- screen-record
- LeetCode Integer to Roman(012)解法總結LeetCode
- LeetCode Roman to Integer(013)解法總結LeetCode
- ES6+ ---- record
- Daily record-SeptemberAI
- Learn and Record12
- Camera List Record - 120
- LeetCode - 解題筆記 - 12 - Integer to RomanLeetCode筆記
- 實戰 Java 16 值型別 Record - 2. Record 的基本用法Java型別
- Roman to Integer 羅馬數字轉整數
- Travel Notes-Record mood
- Homework record-Simple sorting
- 13. 分組資料
- 程式碼審查清單:Java併發 - Roman LeventovJava
- BULK In-BIND與RECORD(轉)
- Renovation Tour-Record my home
- Java 21 新特性:Record PatternsJava
- Record It for Mac錄屏軟體Mac
- gorm忽略報錯: record not foundGoORM
- Java 16 新特性:record類Java
- Erlang中的Record詳解
- 淺析 record 使用場景
- 【深基13.例1】查詢
- record:記錄(帶名元組)
- 不好分類的好題Record
- PerconaXtraDBClusterGCache和Record-Set快取GC快取
- 13. 羅馬數字轉整數
- Redis 實戰 —— 13. 擴充套件 RedisRedis套件
- JDK14 新增關鍵字——recordJDK
- 如何處理 No DMARC Record Found 問題
- 產品路線圖要避免的10個錯誤 - roman
- 13. iOS開發小細節--OC篇iOS