Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

13. Roman to Integer

Posted on 2020-06-14 | Edited on 2021-01-22

LeetCode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public int romanToInt(String s) {
int len=s.length();
int[] tmp=new int[len];
for(int i=0,j=0;i<len;i++){
switch(s.charAt(i)){
case 'I':tmp[j]=1;j++;break;
case 'V':tmp[j]=5;j++;break;
case 'X':tmp[j]=10;j++;break;
case 'L':tmp[j]=50;j++;break;
case 'C':tmp[j]=100;j++;break;
case 'D':tmp[j]=500;j++;break;
case 'M':tmp[j]=1000;j++;break;
}
}
int sum=0;
for(int i=0;i<len;i++){
if(i+1<len&&tmp[i]<tmp[i+1]){
sum+=tmp[i+1]-tmp[i];
i++;
}else{
sum+=tmp[i];
}
}
return sum;
}
}
<1…159160161…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%