Lesson 02 of Likou’s question brushing class-Divide and Conquer and Backtracking Algorithm

Divide and Conquer (Example 1) The letter combination of the nine-key telephone number

Phone Number Character Combinations Given a string containing only the digits 2-9, return all the character combinations it can represent. Answers can be returned in any order. The mapping of numbers to letters is given below (same as phone keys). Note 1 does not correspond to any character⺟

• 示例 1: 输⼊:digits = "23" 输出:[“ad","ae","af","bd","be","bf","cd","ce","cf"]

• Example 2: Input: digits = "" Output: []

• Example 3: Input: digits = "2" Output: ["a","b","c"]

leetcode17

d={2:'abc',3:'def',4:'ghi',
   5:'jkl',6:'mno',7:'pqrs',
   8:'tuv',9:'wxyz'
   }

res=[]
def gene(digits,i,s):
    if len(digits)==len(s):  #
        res.append(s)
        return

    m=d[int(digits[i])]   # 获得对应字母 digits[i]为str类型

    for a in m:
        gene(digits,i+1,s+a)  # 进入下一层  i加1 递归下一个位置 s+a保存当前字母


digits='29'   # str类型
gene(digits,0,'')
print(res)

Don't quite understand

 Divide and conquer (Example 2) implements pow(x, n), that is, calculates the nth power function of x

• Example 1:

• Input: 2.00000, 10

• Output: 1024.00000

• Example 2:

• Input: 2.10000, 3

• Output: 9.26100

• Example 3:

• Input: 2.00000, -2

• Output: 0.25000

• Explanation: 2-2 = 1/22 = 1/4 = 0.25

def pow(x,n):
    if n==1:
        return x

    y=pow(x,n//2)
    if n%2==0:
        return y*y
    else:
        return y*y*x

Don't quite understand

 Backtracking (Example 3) Eight Queens Problem

Place 8 queens on an 8×8 chess board so that they cannot attack each other. That is, no two queens can be in the same row, column or diagonal. How many ways are there to place them?

Backtracking method: If there is a conflict, resolve the conflict. If there is no conflict, go forward. If there is no way to go back, the answer will be the end. 

'''


递归参数:
     n: n皇后
     i: 递归到第几行 0
     l: 记录路径结果 [-1]*n


标记list:  lie=[]
           zXie=[]
           fXie=[]
'''

lie=[]
zXie=[]
fXie=[]
res=[]
def que(n,i,l):
    if n==i:
        res.append(l[:])
        return

    for j in range(n):  # 查找i行存放的位置
        if (j not in lie) and (i+j not in zXie) and (i-j not in fXie):  # j为可以放
            lie.append(j)
            zXie.append(i+j)
            fXie.append(i-j)
            l[i]=j
            que(n,i+1,l)
            lie.remove(j)
            zXie.remove(i+j)
            fXie.remove(i-j)
            l[i]=-1

l=[-1]*4
que(4,0,l)
print(res)

Backtracking (Example 4) to find subsets

You are given an integer array nums. The elements in the array are different from each other. Returns all possible subsets of this array.

The solution set cannot contain duplicate subsets. You can return the solution sets in any order.

Example 1:

Input: nums = [1,2,3]

Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

Example 2:

Input: nums = [0]

Output: [[],[0]]

'''

i: 下一次开始的位置
n: nums的长度
l:记录结果  []

'''

res=[]  # 最终结果
def gene(nums,i,n,l):
    res.append(l)
    for j in range(i,n):
        gene(nums,j+1,n,l+[nums[j]])

nums=[1,2,3]
n=len(nums)
gene(nums,0,n,[])
print(res)

 Backtracking (Example 5) Most Elements

Given an array of size n, find the majority of its elements. Majority elements refer to elements that appear more than ⼊ n/2 ⌋ times in the array.

You can assume that arrays are non-empty and that there is always a majority of elements in a given array.

Example 1:

Input: [3,2,3]

Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]

Output: 2

LeetCode 169. Most elements

Didn't understand the question

Guess you like

Origin blog.csdn.net/Albert233333/article/details/132915775