Leetcode 13 Roman to Integer
来源:互联网 发布:肯尼迪遇刺档案 知乎 编辑:程序博客网 时间:2024/06/16 00:27
Leetcode 13 Roman to Integer
//罗马数字采用七个罗马字母作数字//即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)#include <string>using namespace std;class Solution {public: int romanNumber(char c){ switch (c){ case'I': return 1; case'X': return 10; case'C': return 100; case'M': return 1000; case'V': return 5; case'L': return 50; case'D': return 500; } } int romanToInt(string s) { int sum = 0; int max = 1; for (int i = s.size() - 1; i >=0 ; i--){ if (romanNumber(s[i]) >= max){ max = romanNumber(s[i]); sum += max; } else{ sum -= romanNumber(s[i]); } } return sum; }};/*用map比较明智啊class Solution {public: int romanToInt(string s) { vector<pair<char, int>> map; map.push_back({'I',1}); map.push_back({'V',5}); map.push_back({'X',10}); map.push_back({'L',50}); map.push_back({'C',100}); map.push_back({'D',500}); map.push_back({'M',1000}); int j, last=-1, ret = 0; for(int i=s.size()-1;i>=0;i--) { for(j=0;j<map.size();j++) { if(map[j].first==s[i]) { if(j<last) { ret = ret-map[j].second; last = -1; } else { ret = ret+map[j].second; last = j; } } } } return ret; } };*/
阅读全文
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-13Roman to Integer
- leetcode 13: Roman to Integer
- Leetcode 13 Roman to Integer
- Invalidate的作用
- MATLAB学习笔记 彩色空间转换
- I/O流及文件
- 测试小故事32:只是好那么一点点
- 开源框架推荐:网速监听—facebook/network-connection-class
- Leetcode 13 Roman to Integer
- 并发、并行、同步、异步、多线程的区别
- LeetCode 20 Valid Parentheses
- ROS探索总结(三)——ROS新手教程
- LeetCode 21 Merge Two Sorted Lists
- TCP首部分析及三次握手
- Android Studio Gradle优化方法
- Codeforces 417C Football【思维】
- mybatis 比较字符串的问题