[LeetCode] 17. Letter Combinations of a Phone Number

</br>


Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.


Input:
Digit string "23"
Output:
["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.


</br>

Solution

Whenever a digit is touched, this digit may represent three or four characters. And when the next digit is touched, we have to iterate every existing string, and then appending the next three or four different characters to the end of those.

This process can be hard to proceed if there is no appropriate method. However, instead of trying to achieve the goal in one pass, we can update the answer every time a digit is touched. When the next digit is touched, we pick out all strings currently in the ArrayList[answer] and individually add new characters behind them. In this way, we can lay out all possible combinations.

In order to make sure the algorithm enter the for loop for the first time, we should initialize the ArrayList[answer] to "", so that the ans.size() is above zero.

One more thing, when we set the ArrayList[answer] to "", we have to take care of the empty situation. If the input is empty [], we should have a if sentence to return empty ArrayList.

The code is shown as below.

Java

public class Solution {
    public List<String> letterCombinations(String digits) {
        
        String[] alphabet = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        ArrayList ans = new ArrayList();
        ArrayList empty = new ArrayList();
        
        //Initialize ans, otherwise we can start the very first for loop.
        ans.add("");
        
        
        if (digits.length() == 0)
            return empty;
            
        for (int i = 0; i < digits.length(); i++){
            ArrayList subans = new ArrayList();
            String chars = alphabet[digits.charAt(i) - '0'];
            
            for (int c = 0; c < chars.length();c++)
                for (int j = 0; j < ans.size();j++)
                    subans.add(ans.get(j) + String.valueOf(chars.charAt(c)));
                
            ans = subans;
        }
        return ans;
    }
}

</br>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末属铁,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子躬翁,更是在濱河造成了極大的恐慌焦蘑,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盒发,死亡現(xiàn)場(chǎng)離奇詭異例嘱,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)宁舰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)明吩,“玉大人间学,你說(shuō)我怎么就攤上這事∮±螅” “怎么了低葫?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)仍律。 經(jīng)常有香客問(wèn)我嘿悬,道長(zhǎng),這世上最難降的妖魔是什么水泉? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任善涨,我火速辦了婚禮窒盐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘钢拧。我一直安慰自己蟹漓,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布源内。 她就那樣靜靜地躺著葡粒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪膜钓。 梳的紋絲不亂的頭發(fā)上嗽交,一...
    開(kāi)封第一講書(shū)人閱讀 49,985評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音颂斜,去河邊找鬼夫壁。 笑死,一個(gè)胖子當(dāng)著我的面吹牛沃疮,可吹牛的內(nèi)容都是我干的掌唾。 我是一名探鬼主播,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼忿磅,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼糯彬!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起葱她,我...
    開(kāi)封第一講書(shū)人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤撩扒,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后吨些,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體搓谆,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年豪墅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了泉手。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡偶器,死狀恐怖媳瞪,靈堂內(nèi)的尸體忽然破棺而出刑峡,到底是詐尸還是另有隱情房蝉,我是刑警寧澤叔遂,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站霎苗,受9級(jí)特大地震影響姆吭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜唁盏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一内狸、第九天 我趴在偏房一處隱蔽的房頂上張望检眯。 院中可真熱鬧,春花似錦昆淡、人聲如沸锰瘸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至港庄,卻和暖如春倔既,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鹏氧。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工渤涌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人把还。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓实蓬,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親吊履。 傳聞我的和親對(duì)象是個(gè)殘疾皇子安皱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容