Leetcode刷题第十天-回溯

🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳Happy New Year🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳

LCP51:烹饪料理

链接:LCP 51. 烹饪料理 - 力扣(LeetCode)

新年第一题,来一道简单的烹饪料理,遍历cookbooks,若全部食材满足当前食物的制作,美味和饱腹感均+当前食物对应数据,如果饱腹感大于等于底线,取最大美味度和,数据还原

 1 class Solution:
 2     def perfectMenu(self, materials: List[int], cookbooks: List[List[int]], attribute: List[List[int]], limit: int) -> int:
 3         if(not cookbooks):  return -1
 4         re=[0,0]
 5         y=[-1]
 6         self.bracktracking(0,materials,cookbooks,attribute,re,limit,y)
 7         return y[0]
 8     def bracktracking(self,index,materials,cookbooks,attribute,re,limit,y):
 9         for i in range(index,len(cookbooks)):
10             flage=True
11             book=cookbooks[i]
12             for j in range(5):
13                 if(book[j]>materials[j]):
14                     flage=False
15                     break
16             if(not flage):  continue
17             re[0]+=attribute[i][0]
18             re[1]+=attribute[i][1]
19             self.help(materials,book,True)
20             self.bracktracking(i+1,materials,cookbooks,attribute,re,limit,y)
21             if(re[1]>=limit and re[0]>y[0]):   y[0]=re[0]
22             re[0]-=attribute[i][0]
23             re[1]-=attribute[i][1]
24             self.help(materials,book,False)
25         
26     def help(self,materials,books,flage):
27         for i in range(5):
28             if(flage):  materials[i]-=books[i]
29             else:   materials[i]+=books[i]
30 
31         
perfectMenu

1863:子集的异或总和

链接:1863. 找出所有子集的异或总和再求和 - 力扣(LeetCode)

😓

 1 class Solution:
 2     def subsetXORSum(self, nums: List[int]) -> int:
 3         if(not nums):   return 0
 4         child,sums=[],[0]
 5         self.brackracking(nums,child,[],0,sums)
 6         return sums[0]
 7     def brackracking(self,nums,child,path,index,sums):
 8         for i in range(index,len(nums)):
 9             path.append(nums[i])
10             self.brackracking(nums,child,path,i+1,sums)
11             sums[0]+=self.get_sums(path)
12             path.pop()
13     def get_sums(self,nums):
14         if(len(nums)==1):   return nums[0]
15         sums=nums[0]
16         for i in range(1,len(nums)):
17             sums=sums^nums[i]
18         return sums
subsetXORSum

0807:无重复字符串排列

链接:面试题 08.07. 无重复字符串的排列组合 - 力扣(LeetCode)

表示不理解😯这题难度为啥是中等

 1 class Solution:
 2     def permutation(self, S: str) -> List[str]:
 3         if not S :  return []
 4         re=[]
 5         uset=[0 for i in S]
 6         self.backtracking(S,re,"",uset)
 7         return re
 8     def backtracking(self,s,re,path,uset):
 9         if(len(path)==len(s)):
10             re.append(path)
11             return
12         for i in range(len(s)):
13             if(uset[i]):    continue
14             uset[i]=1
15             self.backtracking(s,re,path+s[i],uset)
16             uset[i]=0
permutation

0808:有重复字符排列

链接:面试题 08.08. 有重复字符串的排列组合 - 力扣(LeetCode)

 1 class Solution:
 2     def permutation(self, S: str) -> List[str]:
 3         if not S :  return []
 4         re=[]
 5         uset=[0 for i in S]
 6         self.backtracking(sorted(S),re,"",uset)
 7         return re
 8     def backtracking(self,s,re,path,uset):
 9         if(len(path)==len(s)):
10             if(path not in re): re.append(path)
11             return
12         for i in range(len(s)):
13             if(uset[i]):    continue
14             uset[i]=1
15             self.backtracking(s,re,path+s[i],uset)
16             uset[i]=0
permutation

 

posted @ 2024-02-12 14:08  小小小茹茹  阅读(12)  评论(0编辑  收藏  举报