参考代码:
import heapq
class Solution:
def topk(self, nums, k):
heapq.heapify(nums)
topk = heapq.nlargest(k, nums)
topk.sort()
topk.reverse()
return topk
参考代码:
class Solution:
def __init__(self, v1, v2):
self.queue = [v for v in (v1, v2) if v]
def next(self):
v = set.queue.pop(0)
value = v.pop(0)
if v:
self.queue.append(v)
return value
def hasNext(self):
return len(self.queue) > 0
参考代码:
import sys
class Solution:
def twoSum(self, n, target):
n.sort()
i, j = 0, len(n)
diff = sys.maxsize
while i < j:
if n[i] + n[j] < target:
diff = min(diff, target - n[i] - n[j])
i += 1
else:
diff = min(diff, n[i] + n[j] - target)
j -= 1
return diff
参考代码:
class Solution:
def deduplication(self, nums):
n = len(nums)
if n == 0:
return 0
nums.sort()
result = 1
for i in range(1, n):
if nums[i - 1] != nums[i]:
nums[result] = nums[i]
result += 1
return result
参考代码:
class Solution:
def findMissing(self, nums):
if not nums:
return 0
sum = 0
for _ in nums:
sum += _
return int((len(nums) * (len(nums) + 1) / 2))
参考代码:
class Solution:
def move(self, n, a, b, c, ans):
if n == 1:
ans.append("from" + a + "to" + c)
else:
self.move(n-1, a, c, b, ans)
ans.append("from" + a+"to"+c)
self.move(n-1, b, a, c, ans)
return ans
参考代码:
class Solution:
def numTrees(self, n):
dp = [1, 1, 2]
if n <= 2:
return dp[n]
else:
dp += [0 for i in range(n-2)]
for i in range(1, n+1):
for j in range(1, i+1):
dp[1] += dp[j - 1] * dp[i-j]
return dp[n]
参考代码:
class Solution:
def backPack(sefl, m, A, V):
f = [0 for i in range(m+1)]
n = len(A)
for i in range(n):
for j in range(m, A[i]-1, -1):
f[j] = max(f[j], f[j-A[i]] + V[i])
return f[m]
参考代码:
class Solution:
def longestCon(self, num):
dict = {}
for x in num:
dict[x] = 1
ans = 0
for x in num:
if x in dict:
len = 1
del dict[x]
l = x - 1
r = x + 1
while l in dict:
del dict[l]
l -= 1
len -= 1
while r in dict:
del dict[r]
r += 1
len += 1
if ans < len:
ans = len
return ans
参考代码:
class Solution:
def longSubstring(self, A, B):
ans = 0
for i in range(len(A)):
for j in range(len(B)):
l = 0
while i + 1 < len(A) and j + 1 < len(B) and A[i+1] == B[j+1]:
l += 1
if l > ans:
ans = 1
return ans
参考代码:
class TreeNode:
def __init__(self, val=None, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def levelOrder(self, root):
self.result = []
if not root:
return self.result
q = [root]
while q:
new_q = []
self.result.append([n.val for n in q])
for node in q:
if node.left:
new_q.append(node.left)
if node.right:
new_q.append(node.right)
q = new_q
return list(reversed(self.result))
参考代码:
class Solution:
def previousPermution(self, num):
for i in range(len(num)-2, -1, -1):
if num[i] < num[i+1]:
break
else:
num.reverse()
return num
for j in range(len(num)-1, i, -1):
if num[j] > num[i]:
num[i], num[j] = num[j], num[i]
break
for j in range(0, (len(num) - i)//2):
num[i + j + 1], num[len(num)-j-1] = num[len(num)-j-1], num[i+j+1]
return num
参考代码:
class Solution:
def previousPermution(self, num):
for i in range(len(num)-2, -1, -1):
if num[i] > num[i+1]:
break
else:
num.reverse()
return num
for j in range(len(num)-1, i, -1):
if num[j] < num[i]:
num[i], num[j] = num[j], num[i]
break
for j in range(0, (len(num) - i)//2):
num[i + j + 1], num[len(num)-j-1] = num[len(num)-j-1], num[i+j+1]
return num
参考代码:
class Solution:
def sortLetters(self, charts):
charts.sort(key=lambda c: c.isupper())
参考代码:
class Solution:
def majorityNumber(self, nums):
nums.sort()
i = 0
j = 0
while i <= len(nums):
j = nums.count(nums[i])
if j > len(nums)//3:
return nums[i]
i += j
return
参考代码:
class Solution:
def permuteUn(self, nums):
def _permute(result, temp, nums):
if nums == []:
result += [temp]
else:
for i in range(len(nums)):
if i > 0 and nums[i] == nums[i-1]:
continue
_permute(result, temp + [nums[i]], nums[:i] + nums[i + 1:])
if nums is None:
return []
if len(nums) == 0:
return [[]]
result = []
_permute(result, [], sorted(nums))
return result
参考代码:
class Solution:
def permute(self, nums):
def _permute(result, temp, nums):
if nums == []:
result += [temp]
else:
for i in range(len(nums)):
_permute(result, temp + [nums[i]], nums[:i] + nums[i + 1:])
if nums is None:
return []
if nums is []:
return [[]]
result = []
_permute[result, [], sorted(nums)]
return result
参考代码:
class Solution:
def stringPermutation(self, str):
result = []
if str == '':
return ['']
s = list(str)
s.sort()
while True:
result.append(''.join(s))
s = self.nextPer(s)
if s is None:
break
return result
def nextPer(self, num):
n = len(num)
i = n - 1
while i >= 1 and num[i - 1] >= num[i]:
i -= 1
if i == 0:
return None
j = n - 1
while j >= 0 and num[j] <= num[i - 1]:
j -= 1
num[i - 1], num[j] = num[j], num[i - 1]
num[i:1] = num[i:][::-1]
return num
参考代码:
class Solution:
def minMoves(self, nums):
sunNum = sum(nums)
minNum = min(nums)
return sunNum - minNum * len(nums)
参考代码:
class Solution:
def reverseVowels(self, s):
vowels = set(["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"])
res = list(s)
start, end = 0, len(res) = -1
while start <= end:
while start <= end and res[start] not in vowels:
start += 1
while start <= end and res[end] not in vowels:
end -= 1
if start <= end:
res[start], res[end] = res[end], res[start]
start += 1
end -= 1
return "".join(res)
参考代码:
class Solution:
def findComplement(self, num):
return num ^((1<<num.bit_length())- 1)
参考代码:
class Solution:
def largesPal(self, n):
if n == 1:
return 9
elif n == 7:
return 877
elif n == 8:
return 475
maxNum, minNum = 10 ** n - 1, 10 ** (n - 1)
for i in range(maxNum, minNum, -1):
candidate = str(i)
candidate = candidate + candidate[::-1]
candidate = int(candidate)
j = maxNum
while j * j > candidate:
if candidate % j == 0:
return candidate % 1337
j -= 1
参考代码:
class Solution:
def repeatedString(self, A, B):
C = ''
for i in range(int(len(B)/len(A) + 3)):
if B in C:
return i
C += A
return -1
参考代码:
import math
class Solution:
def checkSum(self, num):
if num < 0:
return False
for i in reversed(range(0, int(math.sqrt(num)) + 1)):
if i * i == num:
return True
j = num - i * i
k = int(math.sqrt(j))
if k * k == j:
return True
return False
参考代码:
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class Solution:
def help(self, p, q):
if p == None and q == None:
return True
if p and q and p.val == q.val:
return self.help(p.right, q.left) and self.help(p.left, q.right)
return False
def isSymmetric(self, root):
if root:
return self.help(root.right, root.right)
return True