88. The combined two ordered arrays, 112 Path sum

Given two ordered arrays of integers and nums2 nums1, incorporated into the nums2 nums1 in an ordered array such as a num1.

Description:

And initializing the number of elements nums1 nums2 of m and n.
You can assume nums1 sufficient space (space equal to or greater than m + n) to save the elements of nums2.
Example:

Input:
nums1 = [1,2,3,0,0,0], m =. 3
nums2 = [2,5,6], n-=. 3

Output: [1,2,2,3,5,6]

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/merge-sorted-array
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.


class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
while m > 0 and n > 0:
if nums1[m-1] < nums2[n-1]:
nums1[m-1+n] = nums2[n-1]
n = n-1
else:
nums1[m-1+n], nums1[m-1] = nums1[m-1], nums1[m-1+n]
m = m-1
if m==0 and n>0:
nums1[:n] = nums2[:n]

112. The sum of the path

Given a binary tree and a destination and determines whether the root node to the leaf node in the tree path exists, all the nodes on this route, and equal to the target value is added.

Description: leaf node is a node has no child nodes.

Example: 
Given the following binary tree, and the target and sum = 22,

5
/ \
48
/ / \
11134
/ \ \
721
returns true, because the presence of the target path and the root node to the leaf node 22 5-> 4-> 11-> 2.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/path-sum
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

# 112.路径总和
class TreeNode(object):
def __init__(self):
self.val = x
self.left = None
self.right = None


class Solution:
def hasPathSum(self, root, sum):
if root is None:
return False
if root.left is None and root.right is None and root.val == sum:
return True
else:
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)

Guess you like

Origin www.cnblogs.com/xqy-yz/p/11432551.html