Letter Combinations of a Phone Number

来源:互联网 发布:土豆视频windows客户端 编辑:程序博客网 时间:2024/06/05 11:34
  1. 题目
    Given a digit string excluded 01, return all possible letter combinations that the number could represent.
    给一个不包含01的数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合。
    这里写图片描述

  2. 算法
    非递归:依次读取数字,然后把数字可以代表的字符依次加到当前的所有结果中,然后进入下一次迭代。
    比如“234”这个字符串,我可以先将0…1的所有排列找到–>{“a”, “b”, “c”}
    再进一步将0…2的所有排列找到–>{“ad”, “ae”,”af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”}
    如此循环…直到字符串末尾。

    public ArrayList<String> letterCombinations(String digits) {        // Write your code here        ArrayList<String> res = new ArrayList<String>();        if (digits == null || digits.length() == 0) {            return res;        }        res.add("");        for (int i = 0; i < digits.length(); i++) {    //遍历每个数子            String letter = getLetters(digits.charAt(i));   //遍历一个数字,扩充3或4倍,重新建立链表            ArrayList<String> newRes = new ArrayList<String>();            for (int j = 0; j < res.size(); j++) {  //每个链表元素都要加                for (int k = 0; k < letter.length(); k++) {                    newRes.add(res.get(j) + Character.toString(letter.charAt(k)));                }            }            res = newRes;        }        return res;    }    public String getLetters(char digit) {        switch (digit) {            case '2':                return "abc";            case '3':                return "def";             case '4':                return "ghi";            case '5':                return "jkl";             case '6':                return "mno";            case '7':                return "pqrs";              case '8':                return "tuv";            case '9':                return "wxyz";                case '0':                return " ";            default:                return "";        }    }

递归:我们递归每个数子,每个递归中遍历每个数子代表的字母,递归完条件是没有数子了,递归完后把结果加到链表上

    public ArrayList<String> letterCombinations(String digits) {        // Write your code here        ArrayList<String> res = new ArrayList<String>();        if (digits == null || digits.length() == 0) {            return res;        }        helper(digits, "", 0, res);        return res;    }    //digits代表要递归数字  comb代表组合,index代表目前递归的位置,res代表结果    public void helper(String digits, String comb, int index, ArrayList<String> res) {        if (index == digits.length()) {  //递归完条件            res.add(comb);            return;        }        String letter = getLetters(digits.charAt(index));  //递归第index个数字        for (int i = 0; i < letter.length(); i++) {   //遍历每个数字代表的字母            helper(digits, comb + letter.charAt(i), index + 1, res);//把得到的字幕加到组合中,进行下次递归        }    }    public String getLetters(char digit) {        switch (digit) {            case '2':                return "abc";            case '3':                return "def";             case '4':                return "ghi";            case '5':                return "jkl";             case '6':                return "mno";            case '7':                return "pqrs";              case '8':                return "tuv";            case '9':                return "wxyz";                case '0':                return " ";            default:                return "";        }    }
0 0
原创粉丝点击