13. Roman to Integer

Posted luohys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了13. Roman to Integer相关的知识,希望对你有一定的参考价值。

把12题反了过来,不多说了随便写写

 1 public class Solution {
 2     public int RomanToInt(string s) {
 3         int i = 0, result = 0;
 4         while (i < s.Length)
 5         {
 6             if (s[i] == I)
 7             {
 8                 if (i < s.Length - 1)
 9                 {
10                     if (s[i + 1] == V)
11                     {
12                         result += 4;
13                         i += 2;
14                         continue;
15                     }
16                     if (s[i + 1] == X)
17                     {
18                         result += 9;
19                         i += 2;
20                         continue;
21                     }
22                 }
23                 result += 1;
24                 i++;
25             }
26             else if (s[i] == X)
27             {
28                 if (i < s.Length - 1)
29                 {
30                     if (s[i + 1] == L)
31                     {
32                         result += 40;
33                         i += 2;
34                         continue;
35                     }
36                     if (s[i + 1] == C)
37                     {
38                         result += 90;
39                         i += 2;
40                         continue;
41                     }
42                 }
43                 result += 10;
44                 i++;
45             }
46             else if (s[i] == C)
47             {
48                 if (i < s.Length - 1)
49                 {
50                     if (s[i + 1] == D)
51                     {
52                         result += 400;
53                         i += 2;
54                         continue;
55                     }
56                     if (s[i + 1] == M)
57                     {
58                         result += 900;
59                         i += 2;
60                         continue;
61                     }
62                 }
63                 result += 100;
64                 i++;
65             }
66             else if (s[i] == V)
67             {
68                 result += 5;
69                 i++;
70             }
71             else if (s[i] == L)
72             {
73                 result += 50;
74                 i++;
75             }
76             else if (s[i] == D)
77             {
78                 result += 500;
79                 i++;
80             }
81             else
82             {
83                 result += 1000;
84                 i++;
85             }
86         }
87         return result;
88     }
89 }

 

以上是关于13. Roman to Integer的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 13. Roman to Integer

LeetCode之Easy篇 ——(13)Roman to Integer

「Leetcode」13. Roman to Integer(Java)

LeetCode 13. Roman to Integer

13. Roman to Integer

#Leetcode# 13. Roman to Integer