leetcode100题

LeetCode

104 二叉树的最大深度

在这里插入图片描述
思路:

  1. 递归
  2. 深度优先
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        return max(self.maxDepth(root.left),self.maxDepth(root.right))+1

226 反转二叉树

在这里插入图片描述

思路:递归

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
       if root is None:
       		return root
       	else:
       		root.right,root.left = self.invertTree(root.left),self.invertTree(root.right)
       		return root

929 Unique Email Addresses

在这里插入图片描述

class Solution(object):
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        out = []
        for email in emails:
            set1 = email.split('@')
            set2 = set1[0].split('+',1)
            set3 = set2[0].replace('.','')
            set4 = set3 + '@' + set1[1]
            #print set4
            if set4 not in out:
                out.append(set4)
        return len(out)
                       
            

905 Sort Array By Parity

在这里插入图片描述

class Solution(object):
    def sortArrayByParity(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        ji = [];
        ou = [];
        for i in range(len(A)):
            if (A[i] % 2 == 0):
                ji.append(A[i])
            else:
                ou.append(A[i])
        return ji + ou

942 DI String Match

在这里插入图片描述

class Solution(object):
    def diStringMatch(self, S):
        """
        :type S: str
        :rtype: List[int]
        """
        out = []
        tmp = [i for i in range(len(S)+1)]
        left = 0
        right = len(tmp) - 1
        for key in S:
            if key == 'I':
                out.append(left)
                left +=1
            else:
                out.append(right)
                right -=1
        out.append(max(right,left))
        return out

283. Move Zeroes

在这里插入图片描述

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """

        idxs = [idx for idx , num in enumerate(nums) if num == 0]
        print idxs
        for idx in idxs[::-1]:  //将idxs反序
            print idx
            nums.pop(idx)
        nums += [0]*len(idxs)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值