Цена за 48 часов в ленте | 3800,00 |
Цена за 1 час закрепления | N/A |
Взаимопиар | Нет |
Дополнительные условия рекламы | Отсутствуют |
Input: root = [2,1,3]
Output: 1
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
self.maxDepth = -1
self.bottomLeftValue = 0
self.dfs(root, 0)
return self.bottomLeftValue
def dfs(self, current: TreeNode, depth: int):
if not current:
return
if depth > self.maxDepth:
self.maxDepth = depth
self.bottomLeftValue = current.val
self.dfs(current.left, depth + 1)
self.dfs(current.right, depth + 1)
return
Input: c = 5
Output: true
import math
def judgeSquareSum(c: int) -> bool:
a = 0
b = int(math.sqrt(c))
while a <= b:
total = a * a + b * b
if total == c:
return true
elif total < c:
a += 1
else:
b -= 1
return false
Input: n = 5
Output: 5
Explanation:
Here are the non-negative integers <= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
class Solution:
def findIntegers(self, num: int) -> int:
def check(n):
i = 31
while i > 0:
if (n & (1 << i)) != 0 and (n & (1 << (i - 1))) != 0:
return False
i -= 1
return True
count = 0
for i in range(num + 1):
if check(i):
count += 1
return count
Input: nums = [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
class Solution:
def findLHS(self, nums: List[int]) -> int:
count = {}
res = 0
for num in nums:
count[num] = count.get(num, 0) + 1
if num + 1 in count:
res = max(res, count[num] + count[num + 1])
if num - 1 in count:
res = max(res, count[num] + count[num - 1])
return res
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
Explanation: The only common string is "Shogun".
class Solution:
def findRestaurant(self, list1, list2):
map = {}
for i in range(len(list1)):
for j in range(len(list2)):
if list1[i] == list2[j]:
if i + j not in map:
map[i + j] = []
map[i + j].append(list1[i])
min_index_sum = min(map.keys())
return map[min_index_sum]
Input: m = 3, n = 3, ops = [[2,2],[3,3]]
Output: 4
Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.
class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
min_a = m
min_b = n
for op in ops:
min_a = min(min_a, op[0])
min_b = min(min_b, op[1])
return min_a * min_b
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: true
class Solution:
def dist(self, p1, p2):
return (p2[1] - p1[1]) ** 2 + (p2[0] - p1[0]) ** 2
def check(self, p1, p2, p3, p4):
return self.dist(p1, p2) > 0 and \
self.dist(p1, p2) == self.dist(p2, p3) == self.dist(p3, p4) == self.dist(p4, p1) and \
self.dist(p1, p3) == self.dist(p2, p4)
def validSquare(self, p1, p2, p3, p4):
return self.check(p1, p2, p3, p4) or \
self.check(p1, p3, p2, p4) or \
self.check(p1, p2, p4, p3)
Input: expression = "-1/2+1/2+1/3"
Output: "1/3"
def gcd(a, b):
while b:
a, b = b, a % b
return a
class Solution:
def fractionAddition(self, expression: str) -> str:
sign = []
if expression[0] != '-':
sign.append('+')
for char in expression:
if char in '+-':
sign.append(char)
fractions = expression.replace('-', '+-').split('+')
prev_num, prev_den = 0, 1
i = 0
for sub in fractions:
if not sub:
continue
num, den = map(int, sub.split('/'))
g = gcd(prev_den, den)
if sign[i] == '+':
prev_num = prev_num * den // g + num * prev_den // g
else:
prev_num = prev_num * den // g - num * prev_den // g
prev_den = prev_den * den // g
g = abs(gcd(prev_num, prev_den))
prev_num //= g
prev_den //= g
i += 1
return f"{prev_num}/{prev_den}"
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children if children is not None else []
class Solution:
def postorder(self, root: 'Node') -> List[int]:
if not root:
return []
stack, output = [root], []
while stack:
node = stack.pop()
output.append(node.val)
stack.extend(node.children)
return output[::-1]
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
class Solution:
def reverseWords(self, s: str) -> str:
s = list(s)
lastSpaceIndex = -1
length = len(s)
for strIndex in range(length + 1):
if strIndex == length or s[strIndex] == ' ':
startIndex = lastSpaceIndex + 1
endIndex = strIndex - 1
while startIndex < endIndex:
s[startIndex], s[endIndex] = s[endIndex], s[startIndex]
startIndex += 1
endIndex -= 1
lastSpaceIndex = strIndex
return ''.join(s)