File tree Expand file tree Collapse file tree 2 files changed +70
-0
lines changed Expand file tree Collapse file tree 2 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode id=12 lang=csharp
3
+ *
4
+ * [12] Integer to Roman
5
+ */
6
+
7
+ // @lc code=start
8
+ public class Solution {
9
+ public string IntToRoman ( int num ) {
10
+ var result = new StringBuilder ( ) ;
11
+ var map = new Dictionary < int , string >
12
+ {
13
+ { 1000 , "M" } ,
14
+ { 900 , "CM" } ,
15
+ { 500 , "D" } ,
16
+ { 400 , "CD" } ,
17
+ { 100 , "C" } ,
18
+ { 90 , "XC" } ,
19
+ { 50 , "L" } ,
20
+ { 40 , "XL" } ,
21
+ { 10 , "X" } ,
22
+ { 9 , "IX" } ,
23
+ { 5 , "V" } ,
24
+ { 4 , "IV" } ,
25
+ { 1 , "I" } ,
26
+ } ;
27
+ while ( num > 0 ) {
28
+ foreach ( var key in map ) {
29
+ if ( num >= key . Key ) {
30
+ result . Append ( key . Value ) ;
31
+ num -= key . Key ;
32
+ break ;
33
+ }
34
+ }
35
+ }
36
+
37
+ return result . ToString ( ) ;
38
+
39
+ }
40
+ }
41
+ // @lc code=end
42
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode id=8 lang=csharp
3
+ *
4
+ * [8] String to Integer (atoi)
5
+ */
6
+
7
+ // @lc code=start
8
+ public class Solution {
9
+ public int MyAtoi ( string s ) {
10
+ int i = 0 ; // index
11
+ int sign = 1 ; // sign
12
+ int result = 0 ;
13
+ if ( s . Length == 0 ) return 0 ; // empty string
14
+ while ( i < s . Length && s [ i ] == ' ' ) i ++ ; // skip white spaces
15
+ if ( i < s . Length && ( s [ i ] == '+' || s [ i ] == '-' ) ) { // check sign
16
+ sign = ( s [ i ++ ] == '+' ) ? 1 : - 1 ; // skip sign
17
+ }
18
+ while ( i < s . Length && s [ i ] >= '0' && s [ i ] <= '9' ) { // check number
19
+ if ( result > Int32 . MaxValue / 10 || ( result == Int32 . MaxValue / 10 && s [ i ] - '0' > 7 ) ) { // check overflow
20
+ return ( sign == 1 ) ? Int32 . MaxValue : Int32 . MinValue ; // return max or min
21
+ }
22
+ result = result * 10 + ( s [ i ++ ] - '0' ) ; // calculate result
23
+ }
24
+ return result * sign ;
25
+ }
26
+ }
27
+ // @lc code=end
28
+
You can’t perform that action at this time.
0 commit comments