(1)
class Solution: def reverseString(self, s): """ :type s: str :rtype: str """ return ‘‘.join(list(reversed(s))) class Solution: def reverseString(self, s): """ :type s: str :rtype: str """ return s[::-1]
(2)
class Solution: def fizzBuzz(self, n): """ :type n: int :rtype: List[str] """ return [str(i)*(i%3!=0 and i%5!=0)+"Fizz"*(i%3==0)+"Buzz"*(i%5==0) for i in range(1,n+1)] #*指定参数宽度或精度
(3)
from collections import Counter class Solution: def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ a = Counter(nums) for k,i in a.items(): if i < 2: return k
(4)
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def maxDepth(self, root): """ :type root: TreeNode :rtype: int """ return 1 + max(map(self.maxDepth, (root.left, root.right))) if root else 0