[Leetcode] 273. Integer to English Words
来源:互联网 发布:fx网络什么意思 编辑:程序博客网 时间:2024/05/29 06:58
题目:
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”
思路:一看到这个题目想到的是英文数字的规律是从右边3位一组,所以只需把3位一组的数读出加上该对应3位的权(姑且叫权吧,不知道咋说)Billion Million Thousand 即可。
问题就转化为3位数字如何转化为英文。
我的想法百位的数字对应的英文加Hundred,十位数字对应的相应的英文,个位对应的各自的英文数字。
问题即为如何对应相应的英文数字。
我首先想到的是利用map存储对应关系。然后依次读取进行处理。
注意的问题:结果字符串中空格的处理
具体代码:
public class Solution { public String numberToWords(int num) { if(num == 0){ return "Zero"; } StringBuffer result = new StringBuffer(); String s = new Integer(num).toString(); int len = s.length(); //len不是3的整数倍时将前面加0 while(len % 3 != 0){ s = "0" + s; len++; } int count = len / 3; int i = 0; while(i < count){ String sub = s.substring(i*3,3*i+3); //若此时result的已经不为空,在追加后3位的结果时,加" " if(result.equals("") == false){ result.append(" "); } result.append(fun(sub)); //根据该3位的位置相应的追加权重 if(count-i == 4 && sub.equals("000") == false){ result.append(" Billion"); } if(count-i == 3 && sub.equals("000") == false){ result.append(" Million"); } if(count-i == 2 && sub.equals("000") == false){ result.append(" Thousand"); } i++; } String re = result.toString().trim(); while(re.contains(" ") ){ re = re.replaceAll(" ", " "); } return re; } //处理3位 将3位数转化成对应的英文 public String fun(String s){ StringBuffer result = new StringBuffer(); if(s == "000"){ return ""; } Map<String, String> map = new HashMap(); map.put("0",""); map.put("00",""); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); map.put("6", "Six"); map.put("7", "Seven"); map.put("8", "Eight"); map.put("9", "Nine"); map.put("10", "Ten"); map.put("11", "Eleven"); map.put("12", "Twelve"); map.put("13", "Thirteen"); map.put("14", "Fourteen"); map.put("15", "Fifteen"); map.put("16", "Sixteen"); map.put("17", "Seventeen"); map.put("18", "Eighteen"); map.put("19", "Nineteen"); map.put("20", "Twenty"); map.put("30", "Thirty"); map.put("40", "Forty"); map.put("50", "Fifty"); map.put("60", "Sixty"); map.put("70", "Seventy"); map.put("80", "Eighty"); map.put("90", "Ninety"); if(s.charAt(0) != '0'){ result.append(map.get(s.charAt(0)+"")); result.append(" "); result.append("Hundred"); } //11 12 13....19的处理特殊 if(s.charAt(1) == '1'){ String ss = s.charAt(1)+""+s.charAt(2); if(result.equals("") == false){ result.append(" "); } result.append(map.get(ss)); }else{ if(result.equals("")== false){ result.append(" "); } result.append(map.get(s.charAt(1)+"0")); if(s.charAt(2) != '0'){ result.append(" "); } result.append(map.get(s.charAt(2)+"")); } return result.toString().trim(); }}
代码有些冗长,但是思路还算简单,看了一下Leetcode Discuss中最hot的解题方法,是利用数组存储这些对应的英文,利用数组相应的位置对应关系处理的。
0 0
- [leetcode] 273.Integer to English Words
- [LeetCode]273. Integer to English Words
- [leetcode] 273. Integer to English Words
- leetcode:273. Integer to English Words
- leetcode 273. Integer to English Words
- LeetCode 273. Integer to English Words
- LeetCode 273. Integer to English Words
- leetcode.273. Integer to English Words
- [Leetcode] 273. Integer to English Words
- leetcode 273. Integer to English Words
- 【LeetCode】273. Integer to English Words
- [LeetCode]273. Integer to English Words
- LeetCode-273. Integer to English Words
- 【LeetCode】273. Integer to English Words
- leetcode 273. Integer to English Words
- leetcode 273. Integer to English Words
- LeetCode 273. Integer to English Words
- leetcode - Integer to English Words
- Redis基础结构之hashtable(字典实现)
- Unity-C#中关于时间戳的一些方法
- 深入Struts2
- 添加按钮点击事件
- MVP in android
- [Leetcode] 273. Integer to English Words
- java垃圾收集机制(GC)
- unity克隆抛物线型金币的方法
- echarts关系图 unicode编码 phpsql 2016.06.21回顾
- 常用URL
- Unity 激活或关闭选中物体快捷键
- 精灵切割
- Spring 3.X 中各JAR包的用途说明
- 在线手册