1208
Title description:
Give you two strings of the same length, s and t.
Changing the i-th character in s to the i-th character in t requires an overhead of |s[i]-t[i]| (the overhead may be 0), which is the difference between the ASCII code values of the two characters Absolute value.
The maximum budget for changing strings is maxCost. When converting strings, the total cost should be less than or equal to the budget, which also means that the conversion of strings may not be complete.
If you can convert the substring of s into its corresponding substring in t, return the maximum length that can be converted.
If there is no substring in s that can be converted into the corresponding substring in t, 0 is returned.
Example:
Answer:
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
left = right = 0
res = 0
n = len(s)
tmpcost = 0
while right < n:
tmpcost += abs(ord(s[right])-ord(t[right]))
if tmpcost > maxCost:
tmpcost -= abs(ord(s[left])-ord(t[left]))
left += 1
right += 1
res = max(res, right - left)
return res
Sword refers to offer07
Title description:
Enter the results of pre-order traversal and mid-order traversal of a binary tree, please rebuild the binary tree. Assume that the input result of pre-order traversal and middle-order traversal does not contain repeated numbers.
Example:
Answer:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
if not preorder:
return None
loc=inorder.index(preorder[0])
root=TreeNode(preorder[0])
root.left=self.buildTree(preorder[1:loc+1],inorder[:loc])
root.right=self.buildTree(preorder[loc+1:],inorder[loc+1:])
return root
Sword refers to offer55-I
Title description:
Enter the root node of a binary tree and find the depth of the tree. The nodes (including root and leaf nodes) that pass through from the root node to the leaf node in turn form a path of the tree, and the length of the longest path is the depth of the tree.
Example:
Answer:
# 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: TreeNode) -> int:
if not root:
return 0
if not root.left and not root.right:
return 1
L=self.maxDepth(root.left)+1
R=self.maxDepth(root.right)+1
return max(L,R)
Sword refers to offer55-II
Title description:
Enter the root node of a binary tree to determine whether the tree is a balanced binary tree. If the depth of the left and right subtrees of any node in a binary tree does not exceed 1, then it is a balanced binary tree.
Example:
Answer:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
return self.treeHeight(root)>=0
def treeHeight(self,root):
if not root:
return 0
leftHeight=self.treeHeight(root.left)
rightHeight=self.treeHeight(root.right)
if leftHeight>=0 and rightHeight>=0 and abs(leftHeight-rightHeight)<=1:
return max(leftHeight,rightHeight)+1
else:
return -1
Sword refers to offer57-II
Title description:
Input a positive integer target, and output all consecutive positive integer sequences (containing at least two numbers) whose sum is target.
The numbers in the sequence are arranged from smallest to largest, and different sequences are arranged from smallest to largest according to the first number.
Example:
Answer:
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
res=[]
l,r=1,2
while l<r:
a=[]
sum=(l+r)*(r-l+1)/2
if sum<target:
r+=1
elif sum>target:
l+=1
else:
for i in range(l,r+1):
a.append(i)
res.append(a)
l+=1
r+=1
return res