и посмотреть медиа
Golang | LeetCode
и посмотреть медиа
LeetCode tasks in Golang. Solutions with code and explanations.
LeetCode tasks in Golang. Solutions with code and explanations.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
Channel with solutions to popular LeetCode tasks in Go. Focus on optimized algorithms, efficient memory usage, and idiomatic code. Helps prepare for technical interviews in companies that use Golang.
Задача: 273. Integer to English Words Сложность: hard Преобразуйте неотрицательное целое число num в его словесное представление на английском языке. Пример: Input: num = 123 Output: "One Hundred Twenty Three" 👨💻 Алгоритм: 1⃣Обработка чисел до 20 и кратных 10 до 90: Создать массивы или словари для чисел от 1 до 19 и для кратных 10 от 20 до 90. Если число попадает в эти диапазоны, сразу вернуть соответствующее словесное представление. 2⃣Обработка сотен, тысяч, миллионов и миллиардов: Разделить число на группы по три цифры (единицы, тысячи, миллионы, миллиарды). Для каждой группы сформировать словесное представление с использованием рекурсивной функции для чисел от 1 до 999. 3⃣Формирование окончательного результата: Собрать словесное представление всех групп, добавив соответствующие суффиксы (тысячи, миллионы, миллиарды). Соединить все части в одну строку, удалив лишние пробелы. 😎 Решение: package main import ( "strings" "strconv" ) func numberToWords(num int) string { if num == 0 { return "Zero" } belowTwenty := []string{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"} tens := []string{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"} thousands := []string{"", "Thousand", "Million", "Billion"} var result string i := 0 for num > 0 { if num%1000 != 0 { result = helper(num%1000, belowTwenty, tens) + thousands[i] + " " + result } num /= 1000 i++ } return strings.TrimSpace(result) } func helper(num int, belowTwenty, tens []string) string { if num == 0 { return "" } else if num < 20 { return belowTwenty[num] + " " } else if num < 100 { return tens[num/10] + " " + helper(num%10, belowTwenty, tens) } else { return belowTwenty[num/100] + " Hundred " + helper(num%100, belowTwenty, tens) } } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!