(HARD) https://leetcode.com/problems/integer-to-english-words/
Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Example 4:
Input: num = 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
'백의 자리 수를 읽는 공통의 규칙' + '단위'
의 조합으로 생각하면 되겠다는 생각이 들었다.백의 자리 수 + 십의 자리 이하의 수
에 대해 규칙이 또 다르다. 숫자 + hundred
one ~ nine
ten ~ nineteen
10의 자리수 + 1에서 9
'띄어쓰기'
또한 잘 맞춰줘야 된다. 각 블록에 대해 동일한 규칙을 적용해 상황에 따라 띄어쓰기가 아예 없거나, 띄어쓰기가 두 번 이상 나오지 않도록 관리해야 한다. class Solution:
def __init__(self):
self.oneDigitEnglishWord = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
self.tenthEnglishWord = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
self.elevenishEnglishWord = ["", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
# 마지막에 띄어쓰기 없이 출력, 함수 출력값들 사이 연결 시 띄어쓰기 필요
def calcEnglishWord(self, num: int, unit: str):
# 0이면 빈칸 반환
if num == 0:
return ""
ans = ""
#백의 자리에 대해 계산
if num // 100:
ans += self.oneDigitEnglishWord[num // 100] + " Hundred "
#10의 자리 이하에 대해 계산
if 1 <= num % 100 <= 9:
ans += self.oneDigitEnglishWord[num % 100] + " "
elif 11 <= num % 100 <= 19:
ans += self.elevenishEnglishWord[num % 10] + " "
else:
tmp = self.tenthEnglishWord[(num // 10) % 10]
if tmp: #출력값이 존재하지 않을 경우에 발생하는 띄어쓰기 방지
ans += tmp + " "
tmp = self.oneDigitEnglishWord[num % 10]
if tmp:
ans += tmp + " "
return ans + unit
def numberToWords(self, num: int) -> str:
if num == 0:
return "Zero"
real_ans = ""
tmp = self.calcEnglishWord(num // 10**9, "Billion")
real_ans += tmp
tmp = self.calcEnglishWord((num // 10**6) % 1000, "Million")
if real_ans and tmp:
real_ans += " " + tmp
else: #출력값이 존재하지 않을 경우에 발생하는 띄어쓰기 방지
real_ans += tmp
tmp = self.calcEnglishWord((num // 10**3) % 1000, "Thousand")
if real_ans and tmp:
real_ans += " " + tmp
else:
real_ans += tmp
tmp = self.calcEnglishWord(num % 1000, "")
if real_ans and tmp:
real_ans += " " + tmp
else:
real_ans += tmp
#마지막에 생기는 띄어쓰기 지워주기
while len(real_ans) and real_ans[-1] == " ":
real_ans = real_ans[:-1]
return real_ans