1. 程式人生 > >leetcode date2018/4/7

leetcode date2018/4/7

ons != dep int cti sel post com pos

(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

leetcode date2018/4/7