码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 20190626

时间:2020-06-27 00:35:38      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:字符串   tree node   def   none   字符串排序   tco   +=   tle   treenode   

剑指 Offer 55. 平衡二叉树

技术图片

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

class Solution:
    def afterOrder(self,node):
        if node==None:
            return 0,0
        else:
            d1,res1 =self.afterOrder(node.left)
            d2,res2 = self.afterOrder(node.right)
            return max(d1+1,d2+1),max(abs(d2-d1),max(res1,res2))


    def isBalanced(self, root: TreeNode) -> bool:

        d,res = self.afterOrder(root)
      #  print(d,res)
        if res>1:
            return False
        else:
            return  True

  

剑指 Offer 57 - II. 和为s的连续正数序列

技术图片

class Solution:
    def findContinuousSequence(self, target: int) -> List[List[int]]:

        res=[]
        for i in range(2,target):
            if i%2==1:
                if target%i==0 and target//i - i//2>0:
                    start = target//i - i//2
                    end = start+ i
                   # print(res)
                    res.append([j for j in range(start,end)])
            if i%2==0:
                #print(target%i,i//2,target//i - i//2)
                if target%i==i//2 and target//i - i//2+1>0:
                  
                    start =  target//i - i//2+1
                    end = target//i + i//2+1
                    res.append([j for j in range(start,end)])
        res.sort()

        return res

 

剑指 Offer 45. 把数组排成最小的数

技术图片

 

 

1、转成字符串,对字符串排序:

2、两个字符串前后组成一个数,小的排在前

class Solution:
    def minNumber(self, nums: List[int]) -> str:
        for i in range(len(nums)):
            nums[i] = str(nums[i])
        nums.sort()
        t1=‘‘
       # print(nums)
        for i in range(len(nums)-1):
            for j in range(i+1,len(nums)):
                if len(nums[i])<len(nums[j]):
                    if nums[i]+nums[j]>nums[j]+nums[i]:
                        t = nums[j]
                        nums[j] = nums[i]
                        nums[i] = t
         
        for i in range(len(nums)):
            t1 += nums[i]
        #print(nums)
        return t1

  

Leetcode 20190626

标签:字符串   tree node   def   none   字符串排序   tco   +=   tle   treenode   

原文地址:https://www.cnblogs.com/SuckChen/p/13196693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!