[Leetcode] Integer to Chinese Words integers into English words

Source: Internet
Author: User

Convert a non-negative integer to its 中文版 words representation. Given input is guaranteed to being less than 231-1.

For example,

123, "One hundred twenty three" 12345, twelve thousand three hundred forty Five "1234567" one Million-H Undred Thirty four thousand Five hundred sixty Seven "

Hint:

    1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
    2. Group the number by thousands (3 digits). You can write a helper function, which takes a number less than, and convert just that chunk to words.
    3. There is many edge cases. What is some good test cases? Does your code work with input such as 0? Or 1000010? (Middle chunk is zero and should not being printed out)

This problem lets us convert an integer to the English word description, like the method of writing money on a check, my first method is particularly complicated, because I use a few switch statements to list all the words, but I see that the solution of the online gods are enumerated by arrays, particularly ingenious and provincial, Worship in the study. The topic gave enough hints, first told us to 3 a group of processing, and the topic limit the input number range of 0 to 231-1, the highest only to the billion bit, 3 a group also only need to deal with four groups, then we need to handle three a set of numbers of functions, We need to list 1 to 19 English words, put them in an array, and put 20, 30, ... To 90 of the English word list to put in another array, and then we need to write skills, such as a three-digit n, the hundred number is expressed as n/100, the last two digits are represented as n%100, the 10-digit is represented as N%100/10, the single-digit is n%10, and then we see whether the latter two digits is less than 20, The words are removed directly from the array, and if they are greater than or equal to 20, the 10-bit and single-digit words are removed from the two arrays respectively. Then deal with the numbers on the hundred and remember to add hundred. The main function calls four times this help function, and then the middle to insert "thousand", "Million", "billion" to the corresponding position, the last check at the end of whether there is a space, the space is deleted, the return time to check whether the input is 0, is to return ' Zero '. See the code below:

classSolution { Public:    stringNumbertowords (intnum) {        stringres = converthundred (num% +); Vector<string> v = {"thousand","Million","billion"};  for(inti =0; I <3; ++i) {num/= +; Res= num% +? converthundred (num% +) +" "+ V[i] +" "+Res:res; }         while(Res.back () = =' ') Res.pop_back (); returnRes.empty ()?"Zero": Res; }    stringConverthundred (intnum) {Vector<string> v1 = {""," One"," Both","three"," Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; Vector<string> v2 = {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; stringRes; intA = num/ -, B = num% -, C = num%Ten; Res= B < -? V1[B]: v2[b/Ten] + (c?" "+ V1[c]:""); if(A >0) res = V1[a] +"Hundred"+ (b?" "+ Res:""); returnRes; }};

Resources:

Https://leetcode.com/discuss/55268/short-clean-c-code-with-explanation

Leetcode all in one topic summary (continuous update ...)

[Leetcode] integer to Chinese Words integer to English word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.