Giter VIP home page Giter VIP logo

lc-python22's Introduction

LC-Python22

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

June 13, 2023 4h

Congratulations!
This is the 22sh day for leetcode python study. Today we will learn more about backtracking!
The challenges today are about using backtracking as a kind of loop solution. For each loop, the number of possible choices of elements /size of one set defines the width of the tree, and the number of loops we need to have/k defines the depth of the tree, we use recursion to simplify the multiple loops. Don't forget the backtracking process followed!

216.Combination Sum III

Reading link
video
leetcode
Similar to the combination, here we have one more condition, the sum need to equal to the target given.

class Solution:
    def combinationSum3(self, k: int, n: int) -> List[List[int]]:
        result = []    #save the result
        self.backtracking(n, k, 0, 1, [], result)
        return result

    def backtracking(self, targetSum, k, currentSum, startIndex, path,result):
        if currentSum > targetSum:      #cutting the branch
            return
        if len(path) == k:
            if currentSum == targetSum:
                result.append(path[:])
            return
        for i in range(startIndex, 9-(k-len(path))+2):      #cutting branch
            currentSum += i     #process the node
            path.append(i)      #process the node
            self.backtracking(targetSum, k, currentSum, i+1, path,result)
            currentSum -= i     #backtracking
            path.pop()      #backtracking

17.Letter Combinations of a Phone Number

leetcode
Think first, then no idea :(
The number of digits determines the depth of the tree, and 3 letters for each number determines the width of tree.
The solutiion below is so cool and definitely worth reviewing again!

class Solution:
    def __init__(self):
        self.letterMap = [
            "",     #0
            "",     #1
            "abc",     #2
            "def",     #3
            "ghi",     #4
            "jkl",     #5
            "mno",     #6
            "pqrs",     #7
            "tuv",     #8
            "wxyz"     #9
        ]
        self.result = []    #define a global variable to store all results
        self.s = ""     #define a global variable to store each string

    def backtracking(self, digits, index):
        if index == len(digits):
            self.result.append(self.s)
            return
        digit = int(digits[index])     #change number in digits from string input to real number
        letters = self.letterMap[digit]     #get the corresbonding letters for this digit
        for i in range(len(letters)):
            self.s += letters[i]        #add the letter to result string
            self.backtracking(digits, index+1)      #recursion, deal with the next number given in the digits
            self.s = self.s[:-1]    #backtracking, delete the last item added to the result string

    def letterCombinations(self, digits: str) -> List[str]:
        if len(digits) == 0:
            return self.result
        self.backtracking(digits, 0)
        return self.result

lc-python22's People

Contributors

gyjbb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.