From fe9c3220c7407f38b7b67518a2424bbaf2fce4a4 Mon Sep 17 00:00:00 2001 From: Celia_Wu <530081999@qq.com> Date: Sat, 31 Aug 2019 22:18:26 -0400 Subject: [PATCH] 2019-08-31 --- ...61\346\226\207\350\241\250\347\244\272.py" | 48 +++++++++++++++++++ ...25\350\241\214\351\224\256\347\233\230.py" | 8 ++-- 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 "0273.\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272/0273-\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272.py" diff --git "a/0273.\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272/0273-\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272.py" "b/0273.\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272/0273-\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272.py" new file mode 100644 index 0000000..30255cf --- /dev/null +++ "b/0273.\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272/0273-\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272.py" @@ -0,0 +1,48 @@ +class Solution(object): + def numberToWords(self, num): + """ + :type num: int + :rtype: str + """ + def helper(num): + n = int(num) + num = str(n) + if n < 100: + return subhelper(num) + else: + return ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred " + subhelper(num[1:]) if num[1:] != "00" else ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred" + + def subhelper(num): + n = int(num) + l1 = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] + l2 = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] + l3 = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] + if n < 10: + return l1[int(num)] + if 10 <= n < 20: + return l2[n - 10] + if 20 <= n < 100: + return l3[int(num[0]) - 2] + " " + l1[int(num[1])] if num[1] != "0" else l3[int(num[0]) - 2] + res = "" + if num >= 1000000000: + res = helper(str(num)[0]) + " Billion" + if str(num)[1:4] != "000": + res += " " + helper(str(num)[1:4]) + " Million" + if str(num)[4:7] != "000": + res += " " + helper(str(num)[4:7]) + " Thousand" + if str(num)[7:] != "000": + res += " " + helper(str(num)[7:]) + elif num >= 1000000: + res = helper(str(num)[:-6]) + " Million" + if str(num)[-6:-3] != "000": + res += " " + helper(str(num)[-6:-3]) + " Thousand" + if str(num)[-3:] != "000": + res += " " + helper(str(num)[-3:]) + elif num >= 1000: + res = helper(str(num)[:-3]) + " Thousand" + if str(num)[-3:] != "000": + res += " " + helper(str(num)[-3:]) + else: + return helper(str(num)) + + return res \ No newline at end of file diff --git "a/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230.py" "b/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230.py" index 69de22f..9da80b9 100644 --- "a/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230.py" +++ "b/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230.py" @@ -9,8 +9,8 @@ def calculateTime(self, keyboard, word): for i, char in enumerate(keyboard): dic[char] = i - res, last_pos = 0, 0 + res, cur_pos = 0, 0 for char in word: - res += abs(dic[char] - last_pos) - last_pos = dic[char] - return res \ No newline at end of file + res += abs(dic[char] - cur_pos) + cur_pos = dic[char] + return res