# 2020-08-09

#### 99. 恢复二叉搜索树

1 2 5 4 3 可以看到如果乱序了，那么这两个乱序节点肯定是先大后小。对于第一个节点，它属于大的那个，对于第二个节点，它属于小的那个。

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

def recoverTree(self, root):
"""
:type root: TreeNode
:rtype: None Do not return anything, modify root in-place instead.
"""
self.node1 , self.node2 , self.pre = None,None,None
def dfs(root):
if root is None: return
dfs(root.left)
if self.pre and self.pre.val > root.val and self.node1 is None:
self.node1 = self.pre
if self.pre and self.pre.val > root.val and self.node1:
self.node2 = root
self.pre = root
dfs(root.right)
dfs(root)
self.node1.val ,self.node2.val = self.node2.val, self.node1.val

#### 5485. 找出最长的超赞子字符串

class Solution(object):
def longestAwesome(self, s):
"""
:type s: str
:rtype: int
"""
d = {}
d[0] = 0
nowstatus = 0
ans = 0
for idx, c in enumerate(s):
idx+=1
nowstatus = nowstatus ^ 2**(ord(c)-48)

#print(nowstatus)
for j in range(10):
maskstatus = nowstatus ^ 2**j
if maskstatus in d:
ans = max(ans, idx - d[maskstatus])

if nowstatus in d:
ans = max(ans, idx - d[nowstatus])
else:
d[nowstatus] = idx
return ans

#### 1371. 每个元音包含偶数次的最长子字符串

class Solution(object):
def findTheLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
d = dict()
d[0] = 0
nowstatus = 0
ans = 0
for idx, c in enumerate(s):
idx+=1
if c in ['a','e','i','o','u']:
nowstatus = nowstatus ^ 2**(ord(c)-97)
if nowstatus in d:
ans = max(ans , idx-d[nowstatus])
else:
d[nowstatus] = idx
return ans

posted @ 2020-08-09 10:28  樱花庄的龙之介大人  阅读(136)  评论(0编辑  收藏  举报