leetcode记录 13. Roman to Integer
来源:互联网 发布:js删除数组中的对象 编辑:程序博客网 时间:2024/06/08 00:19
还是直接上代码吧,清晰易懂:
ublic int romanToInt(String s) { int result = 0 ; char[] digits = s.toCharArray(); if(digits.length > 0){ /* Initialize value map for Roman chars */ int[] valueMap = new int[26]; valueMap['I' - 'A'] = 1; valueMap['V' - 'A'] = 5; valueMap['X' - 'A'] = 10; valueMap['L' - 'A'] = 50; valueMap['C' - 'A'] = 100; valueMap['D' - 'A'] = 500; valueMap['M' - 'A'] = 1000; int lastIndex = digits[0] - 'A'; result = valueMap[lastIndex]; for(int i = 1 ; i < digits.length ; i++){ int currentIndex = digits[i] - 'A'; // If previous number was smaller in value then remove the previous and decrease the current value by previous value if(valueMap[lastIndex] < valueMap[currentIndex]){ result = result - 2*valueMap[lastIndex] + valueMap[currentIndex] ; }else{ result+= valueMap[currentIndex]; // just add the current value } lastIndex = currentIndex; // update last index } } return result; }
循环中<的情况要减两次2*什么的。
思想是:不管怎么样先加,出现满足的条件,再把之前已经加的值从中减去
0 0
- leetcode记录 13. Roman to Integer
- 个人记录-LeetCode 13. Roman to Integer
- LeetCode 13. Roman to Integer
- [LeetCode]13.Roman to Integer
- LeetCode --- 13. Roman to Integer
- [Leetcode] 13. Roman to Integer
- [leetcode] 13.Roman to Integer
- leetcode 13. Roman to Integer
- leetcode 13. Roman to Integer
- LeetCode 13. Roman to Integer
- [LeetCode]13. Roman to Integer
- Leetcode 13. Roman to Integer
- leetcode 13. Roman to Integer
- leetcode 13. Roman to Integer
- LeetCode *** 13. Roman to Integer
- LeetCode 13. Roman to Integer
- LeetCode 13. Roman to Integer
- 【LeetCode】13. Roman to Integer
- 腾讯云在线直播三
- 开源jxls - java导Excel(多sheet) - 模板文件定义类似jsp的el表达式
- PAT测试 cccc竞赛
- 第十二周项目 阅读程序 c
- DNS中的正向解析与反向解析
- leetcode记录 13. Roman to Integer
- C++拷贝构造函数(深拷贝,浅拷贝)
- Unity3d制作Loading场景进度条
- C# ToString() 格式化大全
- Jxls导出excel的若干方式总结(六)-- 分sheet导出查询记录,每条记录一个sheet
- JS控制图片显示的大小(图片等比例缩放)
- Linux下手动安装配置Tomcat+Mysql+Maven+JDK
- java并发与多线程API学习
- html遮罩实现demo