Skip to content

Commit e3ed08c

Browse files
committed
2020-06-18
1 parent 89b8232 commit e3ed08c

File tree

5 files changed

+93
-11
lines changed

5 files changed

+93
-11
lines changed

1442.形成两个异或相等数组的三元组数目/1442-形成两个异或相等数组的三元组数目.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ def countTriplets(self, arr):
44
:type arr: List[int]
55
:rtype: int
66
"""
7-
leftXOR = [0 for _ in arr] + [0]
8-
97
res = 0
10-
11-
for i, x in enumerate(arr):
12-
leftXOR[i + 1] = leftXOR[i] ^ x
13-
14-
for i in range(len(arr) + 1):
15-
for j in range(i + 1, len(arr) + 1):
16-
for k in range(j + 1, len(arr) + 1):
17-
if leftXOR[i] ^ leftXOR[j] == leftXOR[j] ^ leftXOR[k]:
18-
res += 1
8+
for i in range(len(arr)):
9+
preSum = arr[i]
10+
for j in range(i + 1, len(arr)):
11+
preSum ^= arr[j]
12+
if not preSum:
13+
res += j - i
1914

2015
return res
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def minTime(self, n, edges, hasApple):
3+
"""
4+
:type n: int
5+
:type edges: List[List[int]]
6+
:type hasApple: List[bool]
7+
:rtype: int
8+
"""
9+
from collections import defaultdict
10+
dic = defaultdict(set)
11+
mustVisitNodes = set()
12+
13+
for src, des in edges:
14+
dic[src].add(des)
15+
dic[des].add(src)
16+
17+
def findMustVisitNodesDFS(node, path, visited):
18+
path.append(node)
19+
20+
if hasApple[node]:
21+
for n in path:
22+
mustVisitNodes.add(n)
23+
24+
for child in dic[node]:
25+
if child not in visited:
26+
visited.add(child)
27+
findMustVisitNodesDFS(child, path + [node], visited)
28+
29+
findMustVisitNodesDFS(0, [], set([0]))
30+
31+
return max(0, 2 * (len(mustVisitNodes) - 1))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def maxPower(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
res = 1
8+
pre = s[0]
9+
tmp = 1
10+
for i, ch in enumerate(s):
11+
if i:
12+
if ch == pre:
13+
tmp += 1
14+
else:
15+
pre = ch
16+
tmp = 1
17+
res = max(res, tmp)
18+
return res
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def simplifiedFractions(self, n: int) -> List[str]:
3+
import math
4+
res = set()
5+
6+
for down in range(1, n + 1):
7+
for up in range(1, down):
8+
if math.gcd(up, down) == 1:
9+
res.add(str(up) + "/" + str(down))
10+
11+
return list(res)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution(object):
8+
def goodNodes(self, root):
9+
"""
10+
:type root: TreeNode
11+
:rtype: int
12+
"""
13+
self.res = 0
14+
15+
def dfs(node, maxVal):
16+
if not node:
17+
return
18+
19+
if node.val >= maxVal:
20+
self.res += 1
21+
22+
maxVal = max(maxVal, node.val)
23+
dfs(node.left, maxVal)
24+
dfs(node.right, maxVal)
25+
26+
dfs(root, root.val)
27+
return self.res

0 commit comments

Comments
 (0)