LeetCode | 216. Combination Sum III, 17. Letter Combinations of a Phone Number

216. Combination Sum III

Link: https://leetcode.com/problems/combination-sum-iii/

Description

Find all valid combinations of k numbers that sum up to n such that the following conditions are true:

  • Only numbers 1 through 9 are used.
  • Each number is used at most once.

Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

Approach

  • Initialize two global variables result (store the valid combinations) and path (store the current combination).
  • Initialize a recursive function with parameters k, n, sum (store the current sum of the path) and startIndex (store the current starting integer).
  • If the sum larger than target, which means the sum cannot equal to n, return.
  • If the size of path equals to k and the sum equals to target, add the path to the result.
  • For integers in [startIndex, 9 - (k - path.size()) + 1]:
  • Add the current integer to path and recursively call the function with sum plus the current integer and new startIndex as the current integer plus 1.
  • After the recursive call, the last element of the path list is removed to backtrack and explore other possible combinations.

Solution

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        backTracking(k, 1, 0, n);
        return result;
    }

    private void backTracking(int k, int startIndex, int sum, int target) {
        if (sum > target)
            return;
        if (path.size() == k) {
            if (sum == target) 
                result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
            path.add(i);
            backTracking(k, i + 1, sum + i, target);
            path.remove(path.size() - 1);
        }
    }
}

17. Letter Combinations of a Phone Number

Link: https://leetcode.com/problems/letter-combinations-of-a-phone-number/

Description

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Approach

  • Initialize a global varaible result to store the valid combinations.
  • Initialize a String array nums to represent the mapping of digits to letters accroding to the value of digits.
  • Intialize a recursive function with parameters digits, nums, n (represent the current index in digits) and sb (store the current combination).
  • If the length of sb equals to the length of digits, add the String of sb to result.
  • Find the set of letter corresponds to n.
  • The function then iterates through each letter in the set of letters and appends it to the sb.
  • Then recursively call the function with updated n (n + 1).
  • After the recursive call, remove the last element in sb.

Solution

class Solution {
    List<String> result = new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0)
            return result;
        String[] nums = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        backtracking(digits, nums, 0, new StringBuilder());
        return result;
    }

    private void backtracking(String digits, String[] nums, int n, StringBuilder sb) {
        if (n == digits.length()) {
            result.add(sb.toString());
            return;
        }
        String d = nums[digits.charAt(n) - '0'];
        for (int i = 0; i < d.length(); i++) {
            sb.append(d.charAt(i));
            backtracking(digits, nums, n + 1, sb);
            sb.deleteCharAt(sb.length() - 1);
        }
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值