Skip to content

Commit 53cf795

Browse files
authored
Added tasks 12, 13, 14
1 parent f78b620 commit 53cf795

File tree

10 files changed

+435
-0
lines changed

10 files changed

+435
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace LeetCodeNet.G0001_0100.S0012_integer_to_roman {
2+
3+
using System;
4+
using Xunit;
5+
6+
public class SolutionTest
7+
{
8+
[Fact]
9+
public void IntToRoman()
10+
{
11+
Assert.Equal("III", new Solution().IntToRoman(3));
12+
}
13+
14+
[Fact]
15+
public void IntToRoman2()
16+
{
17+
Assert.Equal("IV", new Solution().IntToRoman(4));
18+
}
19+
20+
[Fact]
21+
public void IntToRoman3()
22+
{
23+
Assert.Equal("IX", new Solution().IntToRoman(9));
24+
}
25+
26+
[Fact]
27+
public void IntToRoman4()
28+
{
29+
Assert.Equal("LVIII", new Solution().IntToRoman(58));
30+
}
31+
32+
[Fact]
33+
public void IntToRoman5()
34+
{
35+
Assert.Equal("MCMXCIV", new Solution().IntToRoman(1994));
36+
}
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace LeetCodeNet.G0001_0100.S0013_roman_to_integer {
2+
3+
using System;
4+
using Xunit;
5+
6+
public class SolutionTest
7+
{
8+
[Fact]
9+
public void RomanToInt()
10+
{
11+
Assert.Equal(3, new Solution().RomanToInt("III"));
12+
}
13+
14+
[Fact]
15+
public void RomanToInt2()
16+
{
17+
Assert.Equal(4, new Solution().RomanToInt("IV"));
18+
}
19+
20+
[Fact]
21+
public void RomanToInt3()
22+
{
23+
Assert.Equal(9, new Solution().RomanToInt("IX"));
24+
}
25+
26+
[Fact]
27+
public void RomanToInt4()
28+
{
29+
Assert.Equal(58, new Solution().RomanToInt("LVIII"));
30+
}
31+
32+
[Fact]
33+
public void RomanToInt5()
34+
{
35+
Assert.Equal(1994, new Solution().RomanToInt("MCMXCIV"));
36+
}
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace LeetCodeNet.G0001_0100.S0014_longest_common_prefix {
2+
3+
using System;
4+
using Xunit;
5+
6+
public class SolutionTest
7+
{
8+
[Fact]
9+
public void LongestCommonPrefix()
10+
{
11+
Assert.Equal("fl", new Solution().LongestCommonPrefix(new string[] { "flower", "flow", "flight" }));
12+
}
13+
14+
[Fact]
15+
public void LongestCommonPrefix2()
16+
{
17+
Assert.Equal("", new Solution().LongestCommonPrefix(new string[] { "dog", "racecar", "car" }));
18+
}
19+
}
20+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace LeetCodeNet.G0001_0100.S0012_integer_to_roman {
2+
3+
// #Medium #String #Hash_Table #Math #Top_Interview_150_Array/String
4+
// #2025_06_20_Time_2_ms_(87.59%)_Space_45.13_MB_(98.99%)
5+
6+
public class Solution
7+
{
8+
public string IntToRoman(int num)
9+
{
10+
var sb = new System.Text.StringBuilder();
11+
int m = 1000;
12+
int c = 100;
13+
int x = 10;
14+
int i = 1;
15+
num = Numerals(sb, num, m, ' ', ' ', 'M');
16+
num = Numerals(sb, num, c, 'M', 'D', 'C');
17+
num = Numerals(sb, num, x, 'C', 'L', 'X');
18+
Numerals(sb, num, i, 'X', 'V', 'I');
19+
return sb.ToString();
20+
}
21+
22+
private int Numerals(System.Text.StringBuilder sb, int num, int one, char cTen, char cFive, char cOne)
23+
{
24+
int div = num / one;
25+
switch (div)
26+
{
27+
case 9:
28+
sb.Append(cOne);
29+
sb.Append(cTen);
30+
break;
31+
case 8:
32+
sb.Append(cFive);
33+
sb.Append(cOne);
34+
sb.Append(cOne);
35+
sb.Append(cOne);
36+
break;
37+
case 7:
38+
sb.Append(cFive);
39+
sb.Append(cOne);
40+
sb.Append(cOne);
41+
break;
42+
case 6:
43+
sb.Append(cFive);
44+
sb.Append(cOne);
45+
break;
46+
case 5:
47+
sb.Append(cFive);
48+
break;
49+
case 4:
50+
sb.Append(cOne);
51+
sb.Append(cFive);
52+
break;
53+
case 3:
54+
sb.Append(cOne);
55+
sb.Append(cOne);
56+
sb.Append(cOne);
57+
break;
58+
case 2:
59+
sb.Append(cOne);
60+
sb.Append(cOne);
61+
break;
62+
case 1:
63+
sb.Append(cOne);
64+
break;
65+
default:
66+
break;
67+
}
68+
return num - (div * one);
69+
}
70+
}
71+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
12\. Integer to Roman
2+
3+
Medium
4+
5+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6+
7+
Symbol Value
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
16+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
17+
18+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
19+
20+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
21+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
22+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
23+
24+
Given an integer, convert it to a roman numeral.
25+
26+
**Example 1:**
27+
28+
**Input:** num = 3
29+
30+
**Output:** "III"
31+
32+
**Example 2:**
33+
34+
**Input:** num = 4
35+
36+
**Output:** "IV"
37+
38+
**Example 3:**
39+
40+
**Input:** num = 9
41+
42+
**Output:** "IX"
43+
44+
**Example 4:**
45+
46+
**Input:** num = 58
47+
48+
**Output:** "LVIII"
49+
50+
**Explanation:** L = 50, V = 5, III = 3.
51+
52+
**Example 5:**
53+
54+
**Input:** num = 1994
55+
56+
**Output:** "MCMXCIV"
57+
58+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
59+
60+
**Constraints:**
61+
62+
* `1 <= num <= 3999`
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace LeetCodeNet.G0001_0100.S0013_roman_to_integer {
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Math
4+
// #Top_Interview_150_Array/String #2025_06_20_Time_1_ms_(99.81%)_Space_49.42_MB_(80.66%)
5+
6+
public class Solution
7+
{
8+
public int RomanToInt(string s)
9+
{
10+
int x = 0;
11+
char y;
12+
for (int i = 0; i < s.Length; i++)
13+
{
14+
y = s[i];
15+
switch (y)
16+
{
17+
case 'I':
18+
x = GetX(s, x, i, 1, 'V', 'X');
19+
break;
20+
case 'V':
21+
x += 5;
22+
break;
23+
case 'X':
24+
x = GetX(s, x, i, 10, 'L', 'C');
25+
break;
26+
case 'L':
27+
x += 50;
28+
break;
29+
case 'C':
30+
x = GetX(s, x, i, 100, 'D', 'M');
31+
break;
32+
case 'D':
33+
x += 500;
34+
break;
35+
case 'M':
36+
x += 1000;
37+
break;
38+
default:
39+
break;
40+
}
41+
}
42+
return x;
43+
}
44+
45+
private int GetX(string s, int x, int i, int i2, char v, char x2)
46+
{
47+
if (i + 1 == s.Length)
48+
{
49+
x += i2;
50+
}
51+
else if (s[i + 1] == v)
52+
{
53+
x -= i2;
54+
}
55+
else if (s[i + 1] == x2)
56+
{
57+
x -= i2;
58+
}
59+
else
60+
{
61+
x += i2;
62+
}
63+
return x;
64+
}
65+
}
66+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
13\. Roman to Integer
2+
3+
Easy
4+
5+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6+
7+
Symbol Value
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
16+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
17+
18+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
19+
20+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
21+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
22+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
23+
24+
Given a roman numeral, convert it to an integer.
25+
26+
**Example 1:**
27+
28+
**Input:** s = "III"
29+
30+
**Output:** 3
31+
32+
**Example 2:**
33+
34+
**Input:** s = "IV"
35+
36+
**Output:** 4
37+
38+
**Example 3:**
39+
40+
**Input:** s = "IX"
41+
42+
**Output:** 9
43+
44+
**Example 4:**
45+
46+
**Input:** s = "LVIII"
47+
48+
**Output:** 58
49+
50+
**Explanation:** L = 50, V= 5, III = 3.
51+
52+
**Example 5:**
53+
54+
**Input:** s = "MCMXCIV"
55+
56+
**Output:** 1994
57+
58+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
59+
60+
**Constraints:**
61+
62+
* `1 <= s.length <= 15`
63+
* `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
64+
* It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`.

0 commit comments

Comments
 (0)