LeetCode题解 第十二周
来源:互联网 发布:sql updata 编辑:程序博客网 时间:2024/06/05 04:56
1.Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Difficulty:Easy
Explanation:
1.只要从大到小将这个整型数字分解,再转换成罗马数字即可。
code:
class Solution {public: int romanToInt(string s) {map<char, int> transmap;int sum = 0;transmap.insert(pair<char, int>((char)'I', 1));transmap.insert(pair<char, int>((char)'V', 5));transmap.insert(pair<char, int>((char)'X', 10));transmap.insert(pair<char, int>((char)'L', 50));transmap.insert(pair<char, int>((char)'C', 100));transmap.insert(pair<char, int>((char)'D', 500));transmap.insert(pair<char, int>((char)'M', 1000));for (int i = 0; i < s.size(); i++){if (s[i] == 'I' || s[i] == 'X' || s[i] == 'C'){if (i+1<s.size()&&transmap[s[i + 1]] > transmap[s[i]]){sum = sum + -1 * transmap[s[i]];continue;}}sum = sum + transmap[s[i]];}return sum;}};
阅读全文
0 0
- LeetCode题解 第十二周
- leetcode-python 第十二周
- leetcode-第十二周
- 第十二周leetcode题
- 第十二周LeetCode
- 第十二周LeetCode
- 第十二周LeetCode
- leetcode【第十二周】简化路径
- 第十二周LeetCode算法题三道
- Leetcode 算法习题 第十二周
- LeetCode题解 第二周
- LeetCode题解 第三周
- LeetCode题解 第五周
- LeetCode题解 第六周
- LeetCode题解 第七周
- LeetCode题解 第八周
- LeetCode题解 第九周
- LeetCode题解 第十周
- 清橙A1019. 枚举字串
- 通过环境变量修改java版本不生效
- 二叉查找树的各种操作(插入、删除、查找、遍历)
- 安装织梦系统出现data无写入权限
- ubuntu 关机重启
- LeetCode题解 第十二周
- 进程调度实验(最高优先数优先算法)的java实现
- Mysql动态创建表
- 织梦重新安装出现 dir
- caffe之python接口实战 :detection 官方教程源码解析
- 手写线程池
- R语言中文社区历史文章整理(类型篇)
- ubuntu 硬盘使用情况
- vue