Skip to content

combination sum and expression add operators #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Expression Add Operators_LC_282.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution:
def addOperators(self, num: str, target: int) -> List[str]:
if len(num) == 0 or not num:
return []
self.result = []
self.recurse(num, target, 0, 0, 0, "")
return self.result

def recurse(self, num, target, index, calc, tail, path):
# base
if index == len(num) and calc == target:
self.result.append(path)
return
if index == len(num):
return

# logic

for i in range(index, len(num)):
if num[index] == "0" and i != index:
continue
curr = int(num[index:i + 1])
if index == 0:
self.recurse(num, target, i + 1, curr, curr, path + str(curr))
else:
# "+" case
self.recurse(num, target, i + 1, calc + curr, int(+curr), path + "+" + str(curr))
# "-" case
self.recurse(num, target, i + 1, calc - curr, -curr, path + "-" + str(curr))
# "*" case
self.recurse(num, target, i + 1, calc - tail + (tail * curr), tail * curr, path + "*" + str(curr))

98 changes: 98 additions & 0 deletions combination_sum_LC_39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#for loop based backtracking
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
self.result=[]
self.recursion(candidates,0,[],target)
return self.result

def recursion(self,candidates,index,path,target):
#base
if index==len(candidates) or target < 0:
return

if target==0:
self.result.append(path.copy())
return
# #logic
for i in range(index,len(candidates)):
# #append

path.append(candidates[i])
# #recursion
self.recursion(candidates,i,path,target-candidates[i])
path.pop()

#for loop recursion
# class Solution:
# def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
# self.result=[]
# self.recursion(candidates,0,[],target)
# return self.result

# def recursion(self,candidates,index,path,target):
# #base
# if index==len(candidates) or target < 0:
# return

# if target==0:
# self.result.append(path)
# return
# # #logic
# for i in range(index,len(candidates)):
# # #append
# newList=path.copy()
# newList.append(candidates[i])
# # #recursion
# self.recursion(candidates,i,newList,target-candidates[i])


# #0/1 recursion
# class Solution:
# def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
# self.result=[]
# self.recursion(candidates,0,[],target)
# return self.result

# def recursion(self,candidates,index,path,target):
# #base
# if index==len(candidates) or target < 0:
# return

# if target==0:
# self.result.append(path)
# return
# #logic
# #0 or non picked values
# self.recursion(candidates,index+1,path,target)
# #append
# newList=path.copy()
# newList.append(candidates[index])
# #1 or picked values
# #recursion
# self.recursion(candidates,index,newList,target-candidates[index])

#0/1 backtracking
# class Solution:
# def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
# self.result=[]
# self.recursion(candidates,0,[],target)
# return self.result

# def recursion(self,candidates,index,path,target):
# #base
# if index==len(candidates) or target < 0:
# return

# if target==0:
# self.result.append(path.copy())
# return
# #logic
# #0 or non picked values
# self.recursion(candidates,index+1,path,target)
# #append
# path.append(candidates[index])
# #1 or picked values
# #recursion
# self.recursion(candidates,index,path,target-candidates[index])
# #remove
# path.pop()