Integer to English Words
来源:互联网 发布:js array splice 添加 编辑:程序博客网 时间:2024/06/05 18:27
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
For example,
123 -> "One Hundred Twenty Three"12345 -> "Twelve Thousand Three Hundred Forty Five"1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Hint:
- Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
- Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
- There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
思路:当年去FB onsite被鄙视的题目,当时leetcode还没有这题,还是个中国人出的。哎,技不如人呀。其实很简单,做过,确实很简单,但是没有做过现场推导还是要点时间的,可惜FB就make assumption,你就做过了。主要技巧就是,可视化之后,发现有规律,就是每3个是一个pattern,然后,分别要处理<10, 10~20, 20~100之间的特殊case,然后后面的就是调用前面的array,计算重复问题。
首先要知道2^31-1,最多是10个digit,也就是最多到billion级别,billion , million, thousand. 2,147,483,647
public class Solution { String[] belowTen = {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; String[] teens ={"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; String[] belowHundreds = {"","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; public String numberToWords(int num) { if(num < 0) return null; if(num == 0) return "Zero"; return helper(num); } public String helper(int num) { String result = new String(); if(num < 10) result = belowTen[num]; else if(num < 20) result = teens[num%10]; else if(num < 100) result = belowHundreds[num/10] +" "+ belowTen[num%10]; else if(num < 1000) result = belowTen[num/100] + " Hundred " + helper(num%100); 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(); }}
0 0
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- leetcode - Integer to English Words
- [leetcode] Integer to English Words
- Integer to English Words -- leetcode
- LeetCode273:Integer to English Words
- sqlserver 事务try-catch
- JavaScript DOM 属性
- jquery 自定义插件
- Android 直接显示yuv数据 通过Awesomeplayer方式直接显示(三)
- java System.arraycopy
- Integer to English Words
- Android Studio使用git的方法
- Android开发人员不得不收集的代码
- yield 的用法
- android群英传笔记——音频条形图(仿音频动画,无监听音频输入)
- jquery_validate 自定义样式,自定义校验方式
- JavaScript的位置
- 在Android平台上搭建Qualcomm的FastCv
- 1000亿以内的素数统计(Meisell-Lehmer算法)