Leetcode 13
2016-03-31 14:38
225 查看
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
public:
int romanToInt(string s) {
int m=0,i=0,j=0,k=0;
while (s[m]!='\0')
m++;
// int *w=new int[];
int w[m];
while(s[i]!='\0')
{
if(s[i]=='I')
w[i]=1;
else if(s[i]=='V')
w[i]=5;
else if(s[i]=='X')
w[i]=10;
else if(s[i]=='L')
w[i]=50;
else if(s[i]=='C')
w[i]=100;
else if(s[i]=='D')
w[i]=500;
else if(s[i]=='M')
w[i]=1000;
i++;
}
j=i-1;
k=w[j];
while(j>0)
{
if(w[j]>w[j-1])
k=k-w[j-1];
else
k=w[j-1]+k;
j--;
}
// delete []w;
return k;
}
};
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
public:
int romanToInt(string s) {
int m=0,i=0,j=0,k=0;
while (s[m]!='\0')
m++;
// int *w=new int[];
int w[m];
while(s[i]!='\0')
{
if(s[i]=='I')
w[i]=1;
else if(s[i]=='V')
w[i]=5;
else if(s[i]=='X')
w[i]=10;
else if(s[i]=='L')
w[i]=50;
else if(s[i]=='C')
w[i]=100;
else if(s[i]=='D')
w[i]=500;
else if(s[i]=='M')
w[i]=1000;
i++;
}
j=i-1;
k=w[j];
while(j>0)
{
if(w[j]>w[j-1])
k=k-w[j-1];
else
k=w[j-1]+k;
j--;
}
// delete []w;
return k;
}
};
相关文章推荐
- Release编译出的ocx控件注册失败问题解决
- 递归4_数组逆序
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析]
- 试算平衡表不平
- mysql主从搭建
- mysql 实现 generate_series()
- 日志
- 在Linux程序中如何获得本进程的可执行文件的全路径?
- 判断各种xxx是否为空
- Android 几个ApplicationInfo Info系列类的总结
- jquery jsonp简单实例
- 跨域请求之JSONP 二
- 一个 Linux 上分析死锁的简单方法
- StateListDrawable
- String.valueOf()的一个坑—“null”
- android 中自动更新时间机制
- ubuntu安装PHP5.6
- 设置全局的转场动画
- 对Map类分别按照Key,Value排序,返回值为List对象
- mac eclipse maven tomcat 运行错误 tomcat HTTP Status 404