摘要:一个数字分为提取整十,提取再处理剩下小于的部分。大于的数字,有以内的按上一行的方法处理。这样穷举出所有情况的好处,就是不用处理这种特殊情况,变得更迅速高效,代码也简洁。
Convert a non-negative integer to its english words representation.
123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
逻辑本身不是太难。这题的特点就是英语是以1000为单位进行操作的,不像汉语用“万”。 英语的另一个特点就是小于20的数字要特殊对待,整十的数字也要特殊对待。 一个数字num, 分为num<20, num<100提取整十,num<1000提取hundreds,再处理剩下小于100的部分。 大于1000的数字,有thuansands, million, billion. 1000以内的按上一行的方法处理。 这里另一个地方就是数字的单词拼写,不出错是很难的,所以多多记忆吧。
public class Solution { private static final String[] lessThan20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; private static final String[] tens = {"","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; private static final String[] thousands = {"","Thousand","Million","Billion"}; public String numberToWords(int num) { if(num == 0) return "Zero"; String word = ""; int i = 0; while(num > 0){ if(num%1000 != 0){ word = helper(num%1000) + thousands[i] + " " + word; } num = num/1000; i++; } return word.trim(); } public String helper(int n) { if(n == 0){ return ""; } else if(n<20){ return lessThan20[n] + " "; } else if(n<100){ return tens[n/10] + " " + helper(n%10); } else { return lessThan20[n/100] + " Hundred " + helper(n%100); } } }
12 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
public class Solution { public String intToRoman(int num) { String[] M = {"","M","MM","MMM"}; // M=1000 String[] C = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; //C = 100, D= 500 String[] X = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; // X=10, L =50 String[] I = {"","I","II","III","IV","V","VI","VII","VIII","IX"}; // I= 1, V =5 return M[num/1000]+C[(num%1000)/100] + X[(num%100)/10] + I[(num%10)]; } }
这样穷举出所有情况的好处,就是不用处理IV这种特殊情况,变得更迅速高效,代码也简洁。
相当于简化版的Integer to English Words.
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66935.html
摘要:是啥是谷歌推出的一套视觉设计语言。比如有的可以换皮肤,而每一套皮肤就是一种设计语言,有古典风呀炫酷风呀极简风呀神马的,而就是谷歌风,有兴趣的同学可以学习了解一下官方原版和中文翻译版,这是每一个产品经理的必修教材。 flutter环境和运行环境搭建好之后,可以开始撸码了,然而当你打开VScode,在打开项目文件夹后,摆在你面前的是main.dart被打开的样子,里面七七八八的已经写好了一...
摘要:在草案中增加了对的支持主要作用在两个非常重要的方面语音识别将所说的转换成文本文字语音合成将文本文字读出来而在版本发布后宣布对该特性的支持今天重要介绍第二部分。是一款基于的跨平台的发音支持类库,支持超过种语言和种声音,分为免费版和商业版。 showImg(https://segmentfault.com/img/bVuZqk); 在w3c草案中增加了对Web Speech Api的支持;...
摘要:题目自己的解决方法其他解决方法 1.题目International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: a maps to .-, b maps to -..., c maps to -.-., and...
摘要:题目自己的解决方法其他解决方法 1.题目International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: a maps to .-, b maps to -..., c maps to -.-., and...
摘要:一题目描述空格分隔,逐个反转二题目描述三题目描述当然也可以用的做,不过用双指针更快。 LeetCode: 557. Reverse Words in a String III 一、LeetCode: 557. Reverse Words in a String III 题目描述 Given a string, you need to reverse the order of chara...
阅读 3958·2021-11-22 15:31
阅读 2493·2021-11-18 13:20
阅读 2875·2021-11-15 11:37
阅读 6787·2021-09-22 15:59
阅读 716·2021-09-13 10:27
阅读 3721·2021-09-09 09:33
阅读 1414·2019-08-30 15:53
阅读 2544·2019-08-29 15:37