LEECODE-Roman to Integer
来源:互联网 发布:面试测评软件系统 编辑:程序博客网 时间:2024/05/20 07:19
class Solution {public: int romanToIntChar(char c) { int data=0; switch(c) { case 'I': { data=1; return data; } case 'V': { data=5; return data; } case 'X': { data=10; return data; } case 'L': { data=50; return data; } case 'C': { data=100; return data; } case 'D': { data=500; return data; } case 'M': { data=1000; return data; } return data; } } int romanToInt(string s) { int length=s.size(); int Res=0,buf=0,pre=0; for(int i=0;i<length;i++) { buf=romanToIntChar(s[i]); if(i==0) { Res+=buf; continue; } pre=romanToIntChar(s[i-1]); if(pre<buf) { Res=Res-2*pre+buf; } else { Res+=buf; } } return Res; }};class Solution {public: int romanToInt(string s) {int Res=0;map<char,int> m={{'I',1};{'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};for(int i=0; i<s.size();i++){if(i==0){Res+=m[s[i]];continue;//注意continue}if(m[s[i]]<=m[s[i-1]]){Res+=m[s[i]];}else{Res=Res-2*m[s[i-1]]+m[s[i]];}}return Res;}};//注意类定义格式
阅读全文
0 0
- LeeCode-Roman to Integer
- LEECODE-Roman to Integer
- Integer to Roman & Roman to Integer
- Integer to Roman & Roman to Integer
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- Integer to Roman and Roman to Integer
- LeetCode Integer to Roman & Roman to Integer
- Roman to Integer/Integer to Roman
- leetcode - roman to integer & integer to roman
- "Roman to Integer" and "Integer to Roman"
- Roman to Integer &&Integer to Roman
- Roman to Integer and Integer to Roman
- Roman to Integer/Integer to Roman
- LeeCode-String to Integer (atoi)
- LeeCode String to Integer (atoi)
- 练习39
- 数字电子技术复习(三)——触发器和时序电路
- javascript:typeof与instanceof区别
- maven工程启动eclipse 报错java.lang.NoClassDefFoundError:
- Zookeeper---概念梳理
- LEECODE-Roman to Integer
- java中IO流的操作
- Exception in thread "main" java.lang.RuntimeException: org.apache.hadoop.security.AccessControlExcep
- Prim算法之我见
- 练习40
- Linux中忘记root密码的解决方法
- Qt5中文乱码解决方法
- (一)、Shiro之简介
- windows下操作linux系统工具