Skip to content

Commit

Permalink
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Dec 13, 2021
2 parents b396e64 + 63e81f5 commit 0df748c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
39 changes: 23 additions & 16 deletions problems/0216.组合总和III.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,29 @@ class Solution {

```py
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = [] #存放结果集
path = [] #符合条件的结果
def findallPath(n,k,sum,startIndex):
if sum > n: return #剪枝操作
if sum == n and len(path) == k: #如果path.size() == k 但sum != n 直接返回
return res.append(path[:])
for i in range(startIndex,9-(k-len(path))+2): #剪枝操作
path.append(i)
sum += i
findallPath(n,k,sum,i+1) #注意i+1调整startIndex
sum -= i #回溯
path.pop() #回溯

findallPath(n,k,0,1)
return res
def __init__(self):
self.res = []
self.sum_now = 0
self.path = []

def combinationSum3(self, k: int, n: int) -> [[int]]:
self.backtracking(k, n, 1)
return self.res

def backtracking(self, k: int, n: int, start_num: int):
if self.sum_now > n: # 剪枝
return
if len(self.path) == k: # len(path)==k时不管sum是否等于n都会返回
if self.sum_now == n:
self.res.append(self.path[:])
return
for i in range(start_num, 10 - (k - len(self.path)) + 1):
self.path.append(i)
self.sum_now += i
self.backtracking(k, n, i + 1)
self.path.pop()
self.sum_now -= i
return
```

## Go:
Expand Down
15 changes: 15 additions & 0 deletions problems/二叉树理论基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ function TreeNode(val, left, right) {
}
```

Swift:
```Swift
class TreeNode<T> {
var value: T
var left: TreeNode?
var right: TreeNode?
init(_ value: T,
left: TreeNode? = nil,
right: TreeNode? = nil) {
self.value = value
self.left = left
self.right = right
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 comments on commit 0df748c

Please sign in to comment.