【LeetCode】Combinations && Letter Combinations of a Phone Number

来源:互联网 发布:咫尺网络 h5 编辑:程序博客网 时间:2024/06/06 10:03
1、Combinations 
Total Accepted: 8363 Total Submissions: 28177 My Submissions
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]
解题思路:组合问题。因为不考虑排列,所以做起来要简单很多。
还是DFS吧,可以求出所有的排列。因为这里只求出一种,如果是多种,再外面在套一层循环即可。
如果针对每一种组合还求排列,直接调用LeetCode中的Permutation就可求出,注意重复与否的问题。
这里还有个问题,就是因为给的是从1到n,如果给的数据是其他的,只需要构成数组,从0开始访问即可。

Java AC

public class Solution {    public ArrayList<ArrayList<Integer>> combine(int n, int k) {        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();        ArrayList<Integer> numList = new ArrayList<Integer>();        dfs(list, numList, n, k, 1);        return list;    }    public void dfs(ArrayList<ArrayList<Integer>> list, ArrayList<Integer> numList,                 int n, int k, int start){        if(numList.size() == k){            list.add(new ArrayList<Integer>(numList));            return;        }        for(int i = start; i <= n; i++){                numList.add(i);            dfs(list, numList, n, k, i+1);            numList.remove(numList.size()-1);        }    }}
2、Letter Combinations of a Phone Number 

Total Accepted: 6921 Total Submissions: 27334 My Submissions
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.
解题思路:这个和1有点像。但是这里是求纯粹的组合问题。思路基本也一致,需要注意的就是给的字符串中有可能包含非数字的字符。

Java AC

public class Solution {    public String array[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};    public ArrayList<String> letterCombinations(String digits) {        ArrayList<String> list = new ArrayList<String>();        if(digits == null){            return list;        }        char numArr[] = digits.toCharArray();        int len = digits.length();        StringBuffer sb = new StringBuffer();        dfs(list,sb,0,numArr,len);        return list;    }    public void dfs(ArrayList<String> list,StringBuffer sb,                int tempLen,char numArr[],int len){        if(tempLen == len){            list.add(sb.toString());            return;        }        if(numArr[tempLen] >= '0' && numArr[tempLen] <= '9' ){            String tempStr = array[numArr[tempLen] - '0'];            int strLen = tempStr.length();            for(int i = 0; i < strLen; i++){                StringBuffer newsb = new StringBuffer(sb);                newsb.append(String.valueOf(tempStr.charAt(i)));                dfs(list,newsb,tempLen+1,numArr,len);            }        }else{            StringBuffer newsb = new StringBuffer(sb);            newsb.append(String.valueOf(numArr[tempLen]));            dfs(list,newsb,tempLen+1,numArr,len);           }    }}

0 0
原创粉丝点击