0%

LeetCode 209 周赛

5531. 特殊数组的特征值

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def specialArray(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n + 1):
c = 0
for num in nums:
if num >= i:
c += 1
if c == i:
return i
return -1

5532. 奇偶树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
def isEvenOddTree(self, root: TreeNode) -> bool:
q, nq = [root], []
flag = True
while q:
if flag:
pre = float('-inf')
for node in q:
if node.val & 1 == 0 or node.val <= pre:
return False
pre = node.val
if node.left:
nq.append(node.left)
if node.right:
nq.append(node.right)
else:
pre = float('inf')
for node in q:
if node.val & 1 == 1 or node.val >= pre:
return False
pre = node.val
if node.left:
nq.append(node.left)
if node.right:
nq.append(node.right)
q, nq = nq, []
flag = not flag
return True

5534. 可见点的最大数目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def visiblePoints(self, points: List[List[int]],
angle: int, location: List[int]) -> int:
import math
angle = angle / 360 * 2 * math.pi
cnt = 0
a = []
for x, y in points:
if x == location[0] and y == location[1]:
cnt += 1
else:
xx, yy = x - location[0], y - location[1]
a.append(math.atan2(yy, xx))
a.sort()
n = len(a)
b = [x + math.pi * 2 for x in a]
a += b
j = 0

ret = 0
for i in range(n):
while j < len(a):
if a[j] - a[i] > angle:
break
j += 1
ret = max(ret, j - i)
return ret + cnt

5533. 使整数变为 0 的最少操作次数

1
2
3
4
5
6
7
class Solution:
def minimumOneBitOperations(self, n: int) -> int:
ans = 0
while n:
ans ^= n
n >>= 1
return ans
1
2
3
4
5
6
class Solution:
def minimumOneBitOperations(self, n: int) -> int:
if n == 0:
return 0
p = n.bit_length()
return (1 << p) - 1 - self.minimumOneBitOperations(n - (1 << (p - 1)))

两种做法