美文网首页
[python] 2019-02-24

[python] 2019-02-24

作者: 原来是酱紫呀 | 来源:发表于2019-02-24 19:31 被阅读0次

.617. Merge Two Binary Trees
(二叉树,不会)
.852. Peak Index in a Mountain Array
.728. Self Dividing Numbers

note:
divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)


617. Merge Two Binary Trees

1) Description

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:

Input:
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output:
Merged tree:
3
/
4 5
/ \ \
5 4 7

2) Solution

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

class Solution:
    def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
        if not t1:
            return t2
        if not t2:
            return t1
        t = TreeNode(t1.val+t2.val)
        t.left = self.mergeTrees(t1.left, t2.left)
        t.right = self.mergeTrees(t1.right, t2.right)
        return t

852. Peak Index in a Mountain Array

1) Description

Let's call an array A a mountain if the following properties hold:

A.length >= 3
There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].

Example 1:

Input: [0,1,0]
Output: 1

Example 2:

Input: [0,2,1,0]
Output: 1

Note:

3 <= A.length <= 10000
0 <= A[i] <= 10^6
A is a mountain, as defined above.

2) Solution

class Solution:
    def peakIndexInMountainArray(self, A: List[int]) -> int:
         return A.index(max(A))

728. Self Dividing Numbers

1) Description

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:

Input:
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

Note:

The boundaries of each input argument are 1 <= left <= right <= 10000.

2) Solution

class Solution:
    def selfDividingNumbers(self, left: int, right: int) -> List[int]:
        A=[]
        for i in range(left, right+1):
            n=1
            for j in str(i):
                
                if int(j)==0 or i % int(j) > 0: 
                    n = 0
                    break
            if n: 
                A.append(i)
        return A

相关文章

网友评论

      本文标题:[python] 2019-02-24

      本文链接:https://www.haomeiwen.com/subject/bbkbyqtx.html