1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 01:36:57 +02:00

cs: add «273. Integer to English Words»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-07 23:30:31 +02:00
parent 520d14ee11
commit 8f762c8776
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,51 @@
public class Solution {
private static readonly List<(int value, string word)> NUMBERS = new List<(int value, string word)>() {
(1000000000, "Billion"),
(1000000, "Million"),
(1000, "Thousand"),
(100, "Hundred"),
(90, "Ninety"),
(80, "Eighty"),
(70, "Seventy"),
(60, "Sixty"),
(50, "Fifty"),
(40, "Forty"),
(30, "Thirty"),
(20, "Twenty"),
(19, "Nineteen"),
(18, "Eighteen"),
(17, "Seventeen"),
(16, "Sixteen"),
(15, "Fifteen"),
(14, "Fourteen"),
(13, "Thirteen"),
(12, "Twelve"),
(11, "Eleven"),
(10, "Ten"),
(9, "Nine"),
(8, "Eight"),
(7, "Seven"),
(6, "Six"),
(5, "Five"),
(4, "Four"),
(3, "Three"),
(2, "Two"),
(1, "One"),
};
public string NumberToWords(int num) {
if (num == 0) {
return "Zero";
}
foreach (var (value, word) in NUMBERS) {
if (num >= value) {
var prefix = (num >= 100) ? NumberToWords(num / value) + " " : "";
var suffix = (num % value == 0) ? "" : " " + NumberToWords(num % value);
return prefix + word + suffix;
}
}
return "";
}
}