*LeetCode-Letter Combinations of a Phone Number

来源:互联网 发布:有关孙悟空的网络电影 编辑:程序博客网 时间:2024/05/16 12:23

错好多 

recursive的下次再写吧

最严重的错误就是 每次循环想要把原来list里面每个string都加一个字母的时候 注意list要重新建 不能for each str in list 然后再里面又在这同一个list上面操作

public class Solution {    public List<String> letterCombinations(String digits) {        List<String> res = new ArrayList<String>();        List<String> nums = Arrays.asList("0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz");        if ( digits == null || digits.length() == 0 )            return res;                res.add("");        for ( int i = 0; i < digits.length(); i ++ ){            List<String> temp = new ArrayList<String>();            for ( String str : res ){                int num = digits.charAt(i) - '0';                int end = 3;                if ( num == 7 || num == 9 )                    end = 4;                for ( int j = 0; j < end; j ++ ){                    String tempstr = str + nums.get(num).charAt(j);                    temp.add(tempstr);                }            }            res = temp;        }        return res;    }}


0 0
原创粉丝点击