国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

273. Integer to English Words

alighters / 2231人閱讀

摘要:一個數字分為提取整十,提取再處理剩下小于的部分。大于的數字,有以內的按上一行的方法處理。這樣窮舉出所有情況的好處,就是不用處理這種特殊情況,變得更迅速高效,代碼也簡潔。

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.

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/66935.html

相關文章

  • flutter筆記3:基礎語法、框架、控件

    摘要:是啥是谷歌推出的一套視覺設計語言。比如有的可以換皮膚,而每一套皮膚就是一種設計語言,有古典風呀炫酷風呀極簡風呀神馬的,而就是谷歌風,有興趣的同學可以學習了解一下官方原版和中文翻譯版,這是每一個產品經理的必修教材。 flutter環境和運行環境搭建好之后,可以開始擼碼了,然而當你打開VScode,在打開項目文件夾后,擺在你面前的是main.dart被打開的樣子,里面七七八八的已經寫好了一...

    draveness 評論0 收藏0
  • 使用 JavaScript 進行單詞發音 Use JavaScript to Speech Your

    摘要:在草案中增加了對的支持主要作用在兩個非常重要的方面語音識別將所說的轉換成文本文字語音合成將文本文字讀出來而在版本發布后宣布對該特性的支持今天重要介紹第二部分。是一款基于的跨平臺的發音支持類庫,支持超過種語言和種聲音,分為免費版和商業版。 showImg(https://segmentfault.com/img/bVuZqk); 在w3c草案中增加了對Web Speech Api的支持;...

    xingqiba 評論0 收藏0
  • 2.leetcode唯一的摩斯密碼

    摘要:題目自己的解決方法其他解決方法 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...

    FreeZinG 評論0 收藏0
  • 2.leetcode唯一的摩斯密碼

    摘要:題目自己的解決方法其他解決方法 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...

    XanaHopper 評論0 收藏0
  • 翻轉字符串的相關題目

    摘要:一題目描述空格分隔,逐個反轉二題目描述三題目描述當然也可以用的做,不過用雙指針更快。 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...

    lykops 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<