Leetcode -- Day 61

來源:互聯網
上載者:User

標籤:

CombinationQuestion 1
CombinationsGiven 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],]
It is also a backstracking tagged problem.
 1     public List<List<Integer>> combine(int n, int k) { 2         List<List<Integer>> rs = new ArrayList<List<Integer>>(); 3         dfs(rs, n, 1, new ArrayList<Integer>(), k); 4         return rs; 5     } 6     private void dfs(List<List<Integer>> rs, int n, int start, List<Integer> path, int k ){ 7         //the terminate condition, return when every process is finished  8         if(k==0){ 9             rs.add(new ArrayList<Integer>(path));10             return;11         }12         //start from 1 to n (i.e. 1,2,3, ... ,n)13         for(int i = start; i<=n; i++){14             path.add(i);15             dfs(rs, n, i+1, path, k-1);16             path.remove(path.size() - 1);17         }18 19     }

 

Question 2Letter Combinations of a Phone NumberGiven 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"].
 1 public class Solution { 2      3     private String[] button; 4     public List<String> letterCombinations(String digits) { 5         button = new String[10]; 6         button[0] = ""; 7         button[1] = ""; 8         button[2] = "abc"; 9         button[3] = "def";10         button[4] = "ghi";11         button[5] = "jkl";12         button[6] = "mno";13         button[7] = "pqrs";14         button[8] = "tuv";15         button[9] = "wxyz";16         17         List<String> result = new ArrayList<String>();18         if (digits == null || digits.length() < 1){19             return result;20         }21         dfs(digits, 0, "", result);22         return result;23     }24     25     public void dfs(String digits, int start, String path, List<String> result){26         if (start >= digits.length()){27             result.add(path);28             return;29         }30         String content = button[digits.charAt(start)-‘0‘];31         for (int i = 0; i < content.length(); i ++){32             dfs(digits, start + 1, path+content.charAt(i), result);33         }34     }35 }

 

 

Leetcode -- Day 61

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.