LeetCode 13 Roman to Integer (C,C++,Java,Python)
来源:互联网 发布:Linux下ifconfig 编辑:程序博客网 时间:2024/06/06 12:40
Problem:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Solution:
时间复杂度O(n)
题目大意:
与12题相反,给一个罗马数字,要求转化为十进制数字
解题思路:
Java源代码(用时749ms):
public class Solution { public int romanToInt(String s) { int index=0,num=0,temp=0; while(index<s.length()){ char c=s.charAt(index++); switch(c){ case 'I':num+=1;temp=1;break; case 'V':num+=temp==1?3:5;break; case 'X':num+=temp==1?8:10;temp=10;break; case 'L':num+=temp==10?30:50;break; case 'C':num+=temp==10?80:100;temp=100;break; case 'D':num+=temp==100?300:500;break; case 'M':num+=temp==100?800:1000;break; } } return num; }}
C语言源代码(用时18ms):
int romanToInt(char* s) { int num=0,temp=0; while(*s){ switch(*s){ case 'I':num+=1;temp=1;break; case 'V':num+=temp==1?3:5;break; case 'X':num+=temp==1?8:10;temp=10;break; case 'L':num+=temp==10?30:50;break; case 'C':num+=temp==10?80:100;temp=100;break; case 'D':num+=temp==100?300:500;break; case 'M':num+=temp==100?800:1000;break; } s++; } return num;}
C++源代码(用时58ms):
class Solution {public: int romanToInt(string s) { int index=0,num=0,temp=0; while(index<s.size()){ char c=s[index++]; switch(c){ case 'I':num+=1;temp=1;break; case 'V':num+=temp==1?3:5;break; case 'X':num+=temp==1?8:10;temp=10;break; case 'L':num+=temp==10?30:50;break; case 'C':num+=temp==10?80:100;temp=100;break; case 'D':num+=temp==100?300:500;break; case 'M':num+=temp==100?800:1000;break; } } return num; }};
Python源代码(用时138ms):
class Solution: # @param {string} s # @return {integer} def romanToInt(self, s): index=0;num=0;temp=0 while index<len(s): c = s[index];index+=1 if c=='I':num+=1;temp=1 elif c=='V':num+=3 if temp==1 else 5 elif c=='X':num+=8 if temp==1 else 10;temp=10 elif c=='L':num+=30 if temp==10 else 50 elif c=='C':num+=80 if temp==10 else 100;temp=100 elif c=='D':num+=300 if temp==100 else 500 elif c=='M':num+=800 if temp==100 else 1000 return num
0 0
- LeetCode 13 Roman to Integer (C,C++,Java,Python)
- LeetCode 12 Integer to Roman(C,C++,Java,Python)
- [LeetCode] 012. Integer to Roman (Medium) (C++/Java/Python)
- [LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)
- 【leetcode c++】13 Roman to Integer
- [leetcode-13]Roman to Integer(C)
- [C++]LeetCode: 16 Roman to Integer
- [C++]LeetCode: 85 Integer to Roman
- C实现 LeetCode->Integer to Roman
- C实现 LeetCode->Roman to Integer
- [leetcode]Roman to Integer C语言
- Leetcode c语言-Integer to Roman
- Leetcode c语言-Roman to Integer
- Leetcode[12-13] Roman to Integer & Integer to Roman(Java)
- 【LeetCode 13】Roman to Integer(Python)
- 【Leetcode】【python】Integer to Roman
- 【Leetcode】【python】Roman to Integer
- Leetcode-Roman to Integer-Python
- No connection could be made because the target machine actively refused it 192.168.131.196:1888
- linux 0.0.1
- ORACLE同义词总结synonyms
- zookeeper学习
- RequireJs边学边写:(一)初识RequireJs
- LeetCode 13 Roman to Integer (C,C++,Java,Python)
- CentOS7配置yum源出现的问题
- Android内存优化之二:MAT使用进阶
- eclipse内存溢出问题调试
- Segue区别及视图的切换
- Spring AOP面向切面编程
- Spring注解注入
- hbase主键模糊查询
- Android-RelativeLayout布局