LeetCode DAY22(216. Combination Sum III&17. Letter Combinations of a Phone Number)

Preface

This is a new day to continue my backtracking algorithm journey.
Learn something new and keep reviewing what I learnt before.

1. Combination Sum III

LeetCode Link: 216. Combination Sum III
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.

Analysis and Solution

Recursion

LeetCode C++ as followings Recursion

class Solution {
private:
    vector<vector<int>> result; // global variation; set that store results meet the criteria 
    vector<int> path; // global variation; store the single result meet the criteria
    void backtracking(int targetSum, int k, int sum, int startIndex) {//recursion function with targetSum k and sum(process sum) startIndex(indicate next loop, incase repeating loop)
        if (sum > targetSum) { // trim the branch
            return; // return if path.size() == k BUT sum != targetSum 
        }
        if (path.size() == k) {//terminate condition 
            if (sum == targetSum) result.push_back(path);
            return;
        }
        for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) { // for loop with trim the branch
            sum += i; // plus the value 
            path.push_back(i); // plus the value
            backtracking(targetSum, k, sum, i + 1); // recursion i+1 assign to startIndex; next loop
            sum -= i; // backtrack
            path.pop_back(); // backtrack
        }
    }

public:
    vector<vector<int>> combinationSum3(int k, int n) {//main function with two parameters
        result.clear(); 
        path.clear();   
        backtracking(n, k, 0, 1);//import the parameters
        return result;
    }
};

2. Letter Combinations of a Phone Number

LeetCode Link: 17. Letter Combinations of a Phone Number
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.

Analysis and Solution

Recursion

LeetCode C++ as followings Recursion

class Solution {
private:
    const string letterMap[10] = {//build a map to store the numbers and letters with relationships;Two-dimensional array
        "", // 0
        "", // 1
        "abc", // 2
        "def", // 3
        "ghi", // 4
        "jkl", // 5
        "mno", // 6
        "pqrs", // 7
        "tuv", // 8
        "wxyz", // 9
    };
public:
    vector<string> result;//define an array
    string s;//define a string
    void backtracking(const string& digits, int index) {//recursion function with string digits adn index as parameters; index indicates the number that was processed
        if (index == digits.size()) {//terminate condition; 
            result.push_back(s);//collect the result 
            return;
        }
        int digit = digits[index] - '0';        // convert the number to int
        string letters = letterMap[digit];      // Get the character set corresponding to the number
        for (int i = 0; i < letters.size(); i++) {//for loop to traverse
            s.push_back(letters[i]);            // collect the result
            backtracking(digits, index + 1);    // recursion with next loop "index+1"
            s.pop_back();                       // backtrack
        }
    }
    vector<string> letterCombinations(string digits) {
        s.clear();
        result.clear();
        if (digits.size() == 0) {//remove the case0
            return result;
        }
        backtracking(digits, 0);//import the parameters
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值