LeetCode Online Judge 题目C# 练习 - Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

 1         static string[] D1 = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
 2         static string[] D10 = { "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
 3         static string[] D100 = { "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
 4         static string[] D1000 = { "M", "MM", "MMM" };
 5         public static string IntegertoRoman(int num)
 6         {
 7             string ret = "";
 8             int[] arr = new int[4];
 9             //Fill in array 1976 -> arr[0] = 6, arr[1] = 7, arr[2] = 9 and arr[3] = 1
10             for (int i = 0; i <= 3; i++)
11             {
12                 arr[i] = num % (int)Math.Pow(10, i + 1) / (int)Math.Pow(10, i);
13             }
14 
15             //Fill in ret ret = "M" + "CM" + "LXX" + "VI";
16             for (int i = 3; i >= 0; i--)
17             {
18                 if (arr[i] != 0)
19                 {
20                     if (i == 3)
21                         ret += D1000[arr[i] - 1];
22                     if (i == 2)
23                         ret += D100[arr[i] - 1];
24                     if (i == 1)
25                         ret += D10[arr[i] - 1];
26                     if (i == 0)
27                         ret += D1[arr[i] - 1];
28                 }
29             }
30 
31             return ret;
32         }

代码分析:

  可以先问面试官可不可以建这个look up table。如果可以,那这样做就快, 狠, 准了。

  

 1         public static string IntegertoRoman2(int num)
 2         {
 3             StringBuilder ret = new StringBuilder();
 4             int[] arr = new int[4];
 5             //Fill in array 1976 -> arr[0] = 6, arr[1] = 7, arr[2] = 9 and arr[3] = 1
 6             for (int i = 0; i <= 3; i++)
 7             {
 8                 arr[i] = num % (int)Math.Pow(10, i + 1) / (int)Math.Pow(10, i);
 9             }
10             
11             ret.Append(IntegertoRoman2Helper(arr[3], 3));
12             ret.Append(IntegertoRoman2Helper(arr[2], 2));
13             ret.Append(IntegertoRoman2Helper(arr[1], 1));
14             ret.Append(IntegertoRoman2Helper(arr[0], 0));
15 
16             return ret.ToString();
17         }
18 
19         public static string IntegertoRoman2Helper(int num, int level)
20         {
21             string[] map = new string[] { "I", "V", "X", "L", "C", "D", "M" };
22             string s1 = null, s2 = null, s3 = null;
23 
24             if (level == 0)
25             {
26                 s1 = "I"; s2 = "V"; s3 = "X";
27             }
28             else if (level == 1)
29             {
30                 s1 = "X"; s2 = "L"; s3 = "C";
31             }
32             else if (level == 2)
33             {
34                 s1 = "C"; s2 = "D"; s3 = "M";
35             }
36             else if (level == 3)
37             {
38                 s1 = "M";
39             }
40 
41             StringBuilder sb = new StringBuilder();
42 
43             if (num >= 1 && num <= 3)
44             {
45                 for (int i = 1; i <= num; i++)
46                     sb.Append(s1);
47             }
48             else if (num == 4)
49             {
50                 sb.Append(s1 + s2);
51             }
52             else if (num >= 5 && num <= 8)
53             {
54                 sb.Append(s2);
55                 for (int i = 1; i <= num - 5; i++)
56                     sb.Append(s1);
57             }
58             else if (num == 9)
59                 sb.Append(s1 + s3);
60 
61             return sb.ToString();
62         }

代码分析:

  以上代码是没有用look up table的。稍微麻烦一点。

  做个笔记,在C++中如果想用StringBuilder(所谓的SB), 可以用std::stringstream, Append 字符串 就是 ss << "abc" << "def";

  

posted @ 2012-09-13 22:06  ETCOW  阅读(389)  评论(0编辑  收藏  举报