leetcode 13 Roman to Integer C++
2016-05-05 21:20
696 查看
这道题主要是要知道罗马转数字的规则,前边的比后边的小就要减。
int romanToInt(string s) { int a[26] = {0}; a['I'-'A'] = 1; a['V'-'A'] = 5; a['X'-'A'] = 10; a['L'-'A'] = 50; a['C'-'A'] = 100; a['D'-'A'] = 500; a['M'-'A'] = 1000; int sum = 0; int max = 1; for (int i = s.size() - 1; i >= 0; i--) { if (a[s[i]-'A'] >= max) { max = a[s[i]-'A']; sum += a[s[i]-'A']; }else{ sum -= a[s[i]-'A']; } } return sum; }
相关文章推荐
- 【C++】VS2010将写好的程序打包成安装文件发布
- 《高质量C++/C编程指南》代码规范笔记
- PAT (Advanced Level) Practise 1102 Invert a Binary Tree PAT 1102 坑点
- leetcode 92 Reverse Linked List II C++
- C语言内存分配方式
- More Effective C++----异常 & (9)使用析构函数防止资源泄漏
- C++中引用类型
- [Assignment] C++1
- 【C/C++】类中的static成员、static const成员和const成员
- [C/C++] main函数执行前后还会发生什么
- Effective C++ 改善程序与设计的55个具体做法之条款37
- C语言中字符串的格式化
- c语言
- C++异常
- hashtable笔记 c++
- 数组型队列(queue)的使用(支持自定义数据类型)(C++版)
- Leetcode 17. Letter Combinations of a Phone Number
- C++第五次实验-项目1:数组分离
- leetcode 206 Reverse Linked List C++
- 标准C++中的string类的用法总结