摘要:一個(gè)數(shù)字分為提取整十,提取再處理剩下小于的部分。大于的數(shù)字,有以內(nèi)的按上一行的方法處理。這樣窮舉出所有情況的好處,就是不用處理這種特殊情況,變得更迅速高效,代碼也簡潔。
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"
邏輯本身不是太難。這題的特點(diǎn)就是英語是以1000為單位進(jìn)行操作的,不像漢語用“萬”。 英語的另一個(gè)特點(diǎn)就是小于20的數(shù)字要特殊對待,整十的數(shù)字也要特殊對待。 一個(gè)數(shù)字num, 分為num<20, num<100提取整十,num<1000提取hundreds,再處理剩下小于100的部分。 大于1000的數(shù)字,有thuansands, million, billion. 1000以內(nèi)的按上一行的方法處理。 這里另一個(gè)地方就是數(shù)字的單詞拼寫,不出錯(cuò)是很難的,所以多多記憶吧。
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這種特殊情況,變得更迅速高效,代碼也簡潔。
相當(dāng)于簡化版的Integer to English Words.
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/66935.html
摘要:是啥是谷歌推出的一套視覺設(shè)計(jì)語言。比如有的可以換皮膚,而每一套皮膚就是一種設(shè)計(jì)語言,有古典風(fēng)呀炫酷風(fēng)呀極簡風(fēng)呀神馬的,而就是谷歌風(fēng),有興趣的同學(xué)可以學(xué)習(xí)了解一下官方原版和中文翻譯版,這是每一個(gè)產(chǎn)品經(jīng)理的必修教材。 flutter環(huán)境和運(yùn)行環(huán)境搭建好之后,可以開始擼碼了,然而當(dāng)你打開VScode,在打開項(xiàng)目文件夾后,擺在你面前的是main.dart被打開的樣子,里面七七八八的已經(jīng)寫好了一...
摘要:在草案中增加了對的支持主要作用在兩個(gè)非常重要的方面語音識別將所說的轉(zhuǎn)換成文本文字語音合成將文本文字讀出來而在版本發(fā)布后宣布對該特性的支持今天重要介紹第二部分。是一款基于的跨平臺的發(fā)音支持類庫,支持超過種語言和種聲音,分為免費(fèi)版和商業(yè)版。 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...
摘要:一題目描述空格分隔,逐個(gè)反轉(zhuǎn)二題目描述三題目描述當(dāng)然也可以用的做,不過用雙指針更快。 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...
閱讀 4015·2021-11-22 15:31
閱讀 2564·2021-11-18 13:20
閱讀 3141·2021-11-15 11:37
閱讀 7157·2021-09-22 15:59
閱讀 774·2021-09-13 10:27
閱讀 3807·2021-09-09 09:33
閱讀 1473·2019-08-30 15:53
閱讀 2590·2019-08-29 15:37