参考代码:
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class Solution:
def isComplete(self, root):
if root is None:
return True
queue = [root]
index = 0
while index < len(queue):
if queue[index] is not None:
queue.append(queue[index].left)
queue.append(queue[index].right)
index += 1
while queue[-1] is None:
queue.pop()
for q in queue:
if q is None:
return False
return True
参考代码:
class Solution:
def reverseInteger(self, n):
if n == 0:
return 0
neg = 1
if n < 0:
neg, n = -1, -n
revers = 0
while n > 0:
reverse = reverse * 10 + n % 10
n = n // 10
reverse = reverse * neg
if reverse < - (1 << 31) or reverse > (1 << 31) - 1:
return 0
return reverse
参考代码:
class ListNode:
def __init__(self, val):
self.val = val
self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
dummy = ListNode(0)
tmp = dummy
while l1 != None and l2 != None:
if l1.val < l2.val:
tmp.next = l1
else:
tmp.next = l2
l2 = l2.next
tmp = tmp.next
if l1 != None:
tmp.next = l1
else:
tmp.next = l2
return dummy.next
参考代码:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
class Solution:
def cloneTree(self, root):
if root is None:
return None
clone_root = TreeNode(root.val)
clone_root.left = self.cloneTree(root.left)
clone_root.right = self.cloneTree(root.right)
参考代码:
class Solution:
def removeElement(self, A, elem):
j = len(A) - 1
for i in range(len(A) - 1, -1, -1):
if A[i] == elem:
A[i], A[j] = A[j], A[i]
j -= 1
return j + 1
参考代码:
class ListNode(object):
def __int__(self, val):
self.val = val
self.next = None
class Solution:
def nthToLast(self, head, n):
if head is None or n < 1:
return None
cur = head.next
while cur is not None:
cur.pre = head
cur = cur.next
head = head.next
n -= 1
while n > 0:
head = head.pre
n -= 1
return head
参考代码:
class Solution:
def longesWords(self, dictionary):
answer = []
maxLength = 0
for item in dictionary:
if len(item) > maxLength:
maxLength = len(item)
answer = [item]
elif len(item) == maxLength:
answer.append(item)
return answer
参考代码:
class Solution:
def anagram(self, s, t):
set_s = [0] * 256
set_t = [0] * 256
for i in range(0, len(s)):
set_s[ord(s[i])] += 1
for i in range(0, len(t)):
set_t[ord(t[i])] += 1
for i in range(0, 256):
if set_s[i] != set_t[i]:
return False
return True
参考代码:
class Solution:
def anagram(self, s, t):
set_s = [0] * 256
set_t = [0] * 256
for i in range(0, len(s)):
set_s[ord(s[i])] += 1
for i in range(0, len(t)):
set_t[ord(t[i])] += 1
for i in range(0, 256):
if set_s[i] != set_t[i]:
return False
return True
参考代码:
class Solution:
def judeTheLastNumber(self, str):
if str[-1] == -1:
return 2
for i in range(-2, -len(str) - 1, -1):
if str[i] == 0:
return -1 * ((i * -1 + 1) % 2) + 2
return -1 * (len(str) % 2) + 2
参考代码:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
class Solution:
def levelSum(self, root, level):
p = []
self.dfs(root, p, 1, level)
return sum(p)
def dfs(self, root, p, dep, level):
if root is None:
return
if dep == level:
p.append(root.val)
return
self.dfs(root.left, p, dep + 1, level)
self.dfs(root.right, p, dep + 1, level)
参考代码:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
class Solution:
def leafSum(self, root):
p = []
self.dfs(root, p)
return sum(p)
def dfs(self, root, p):
if root is None:
return
if root.left is None and root.right is None:
p.append(root.val)
self.dfs(root.left, p)
self.dfs(root.right, p)
参考代码:
class Solution:
def secondMax(self, nums):
maxValue = max(nums[0], nums[1])
secValue = min(nums[0], nums[1])
for i in range(2, len(nums)):
if nums[i] > maxValue:
secValue = maxValue
maxValue = nums[i]
elif nums[i] > secValue:
secValue = nums[i]
return secValue
参考代码:
class Solution:
def calculate(self, a, operator, b):
if operator == '+':
return a + b
elif operator == '-':
return a - b
elif operator == '*':
return a * b
elif operator == '/':
return a/b
参考代码:
class Solution:
def topKFrequentWords(self, words, k):
dict = {}
res = []
for word in words:
if word not in dict:
dict[word] = 1
else:
dict[word] += 1
sorted_d = sorted(dict.items(), key = lambda x:x[1], reverse=True)
for i in range(k):
res.append(sorted_d[i][0])
return res
参考代码:
class ListNode(object):
def __init__(self, val, next = None) -> None:
self.val = val
self.next = next
class Solution:
def countNodes(self, head):
cnt = 0
while head is not None:
cnt += 1
head = head.next
return cnt
参考代码:
class Solution:
def lowercaseToUpper(self, character):
return chr(ord(character) - 32)
参考代码:
class Solution:
def minPathSum(self, grid):
for i in range(len(grid)):
for j in range(len(grid[0])):
if i == 0 and j > 0:
grid[i][j] += grid[i][j - 1]
elif j == 0 and i > 0:
grid[i][j] += min(grid[i-1][j], grid[i][j-1])
elif i > 0 and j > 0:
grid[i][j] += min(grid[i-1][j], grid[i][j-1])
return grid[len(grid) - 1][len(grid[0]) - 1]
参考代码:
class Solution:
def mergeSortedArray(self, A, m, B, n):
i, j = m - 1, n - 1
t = len(A) - 1
while i >= 0 or j >= 0:
if i < 0 or (j >= 0 and B[j] > A[i]):
A[t] = B[j]
j -= 1
else:
A[t] = A[i]
i -= 1
t -= 1
参考代码:
class Solution:
def findNumbers(self, nums):
n = len(nums)
s = set(nums)
res = [i for i in range(1, n + 1) if i not in s]
return res
参考代码:
class Solution:
def convertToBase7(self, num):
if num < 0:
return '-' + self.convertToBase7(-num)
if num < 7:
return str(num)
return self.convertToBase7(num//7) + str(num % 7)
参考代码:
class Solution:
def capitalizesFirst(self, s):
n = len(s)
s1 = list(s)
if s1[0] >= 'a' and s1[0] <= 'z':
s1[0] = chr(ord(s1[0]) - 32)
for i in range(1, n):
if s1[i -1] == '' and s1[i] != '':
s1[i] = chr(ord(s1[i]) - 32)
return ''.join(s1)
参考代码:
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class Solution:
def longestConsecutive(self, root):
return self.helper(root, None, 0)
def helper(self, root, parent, len):
if root is None:
return len
if parent != None and root.val == parent.val + 1:
len += 1
else:
len = 1
return max(len, max(self.helper(root.left, root, len), self.helper(root.right, root, len)))
参考代码:
class Toy:
def talk(self):
raise NotImplementedError("This method should have implemented")
class Dog(Toy):
def talk(self):
print("Wow")
class Cat(Toy):
def talk(self):
print("Meow")
class ToyFactory:
def getToy(self, type):
if type == "Dog":
return Dog()
elif type == "Cat":
return Cat()
return None
参考代码:
class Solution:
def kthSmallestSum(self, A, B, k):
if not A or not B:
return None
n, m = len(A), len(B)
minheap = [A[0] + B[0], 0, 0]
visited = set([0])
num = None
for _ in range(k):
num, x, y = heapq.heappop(minheap)
if x+1 < n and (x + 1) * m + y not in visited:
heapq.heappush(minheap, (A[x + 1] + B[y], x + 1, y))
visited.add((x + 1) * m + y)
if y+1 < m and x * m + y + 1 not in visited:
heapq.heappush(minheap, (A[x] + B[y + 1], x, y+1))
visited.add(x * m + y + 1)
return num