Problem
Given a non-negative integer n, print the number in words.
ExampleGiven n = 125
Return one hundred twenty five
Solutionclass Solution { private String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; private String[] tenToTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private String[] tens = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; public String convertWords(int num) { if (num == 0) return "zero"; return helper(num); } private String helper(int num) { String result; if (num < 10) result = belowTen[num]; else if (num < 20) result = tenToTwenty[num-10]; else if (num < 100) result = tens[num/10] + " " + belowTen[num%10]; else if (num < 1000) result = belowTen[num/100] + " Hundred " + helper(num%100); else if (num < 10000) result = belowTen[num/1000] + " Thousand " + helper(num%1000); else if (num < 1000000) result = helper(num/1000) + " Thousand " + helper(num%1000); else if (num < 1000000000) result = helper(num/1000000) + " Million " + helper(num%1000000); else result = helper(num/1000000000) + " Billion " + helper(num%1000000000); return result.trim().toLowerCase(); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71520.html
LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...
Problem Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, great acting skills a...
Problem Given a dictionary, find all of the longest words in the dictionary. Example Given { dog, google, facebook, internationalization, blabla } the longest words are(is) [internationaliz...
Problem Implement a trie with insert, search, and startsWith methods. Example insert(lintcode) search(code) // return false startsWith(lint) // return true startsWith(linterror) // return false insert...
摘要:使用,利用其按层次操作的性质,可以得到最优解。这样可以保证这一层被完全遍历。每次循环取出的元素存为新的字符串。一旦找到和相同的字符串,就返回转换序列长度操作层数,即。 Problem Given two words (start and end), and a dictionary, find the length of shortest transformation sequence...
阅读 2567·2023-04-25 17:33
阅读 647·2021-11-23 09:51
阅读 2950·2021-07-30 15:32
阅读 1394·2019-08-29 18:40
阅读 1939·2019-08-28 18:19
阅读 1464·2019-08-26 13:48
阅读 2236·2019-08-23 16:48
阅读 2274·2019-08-23 15:56