mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «273. Integer to English Words»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
520d14ee11
commit
8f762c8776
1 changed files with 51 additions and 0 deletions
51
cs/integer-to-english-words.cs
Normal file
51
cs/integer-to-english-words.cs
Normal 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 "";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue