當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
[LeetCode][JavaScript]Roman to Integer
生活随笔
收集整理的這篇文章主要介紹了
[LeetCode][JavaScript]Roman to Integer
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
https://leetcode.com/problems/roman-to-integer/
?
?
?
?
羅馬數字轉阿拉伯數字。
從后往前掃,如果當前的數大于之前的數,加上這個數,反之減去當前的數。
1 /** 2 * @param {string} s 3 * @return {number} 4 */ 5 var romanToInt = function(s) { 6 var map = {}; 7 map["I"] = 1; map["V"] = 5; map["X"] = 10; map["L"] = 50; 8 map["C"] = 100; map["D"] = 500; map["M"] = 1000; 9 var res, tmp = 0; 10 for(var i = s.length - 1; i >= 0; i--){ 11 if(!res){ 12 res = map[s[i]]; 13 continue; 14 } 15 if(map[s[i]] >= map[s[i + 1]]){ 16 res += map[s[i]]; 17 }else{ 18 res -= map[s[i]]; 19 } 20 } 21 return res; 22 };?
轉載于:https://www.cnblogs.com/Liok3187/p/4770215.html
總結
以上是生活随笔為你收集整理的[LeetCode][JavaScript]Roman to Integer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈C++设计模式之工厂方法(Facto
- 下一篇: 国行 lg g3 D858 刷 lg g