Skip to content

Commit 0744014

Browse files
committed
leetcode #437
1 parent 619e4f9 commit 0744014

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

flipBitToWin.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@ def flipBitToWin(number):
33
n_b = bin(number)[2:]
44

55
arr = n_b.split('0')
6-
l = 0
6+
lenght = 0
7+
8+
print(arr)
79

810
for i in range(len(arr)-1):
911

10-
if len(arr[i]) + len(arr[i+1]) > l:
11-
l = len(arr[i]) + len(arr[i+1])
12+
seq = len(arr[i]) + len(arr[i+1]) + 1
13+
14+
if seq > lenght:
15+
lenght = seq
16+
17+
return lenght
18+
19+
print(flipBitToWin(1775))
1220

13-
l+=1
1421

15-
return l
22+
# 11011101111
1623

17-
print(flipBitToWin(1775))
24+
# 11 2
25+
# 111 3
26+
# 1111 4

path-sum-iii.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#https://leetcode.com/problems/path-sum-iii/submissions/
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
10+
class Solution:
11+
12+
def DepthFirstSearch(self, root, sum):
13+
ans = 0
14+
15+
if not root:
16+
return ans
17+
18+
if root.val == sum:
19+
ans += 1
20+
21+
ans += self.DepthFirstSearch(root.left, sum - root.val)
22+
ans += self.DepthFirstSearch(root.right, sum - root.val)
23+
24+
return ans
25+
26+
def pathSum(self, root: 'TreeNode', sum: 'int') -> 'int':
27+
28+
if not root:
29+
return 0
30+
31+
else:
32+
ans = self.DepthFirstSearch(root, sum) + self.pathSum(root.left, sum) + self.pathSum(root.right, sum)
33+
34+
return ans

0 commit comments

Comments
 (0)