本节只写自己做的leedcode,只是用来当笔记,里面记录的方法有自己想的,也有来自各位大神的思路
1.重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
限制:
0 <= 节点个数 <= 5000
思路:思路详细步骤地址
# 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 len(preorder)==0:
return None
curr_root_val = preorder[0]
#查找中序遍历的根节点的索引值
curr_root_index_in_inorder = inorder.index(curr_root_val)
#创建根节点
rootNode = TreeNode(curr_root_val)
#先序遍历和中序遍历的左子树
left_tree_pre = preorder[1:1+curr_root_index_in_inorder]
left_tree_in = inorder[:curr_root_index_in_inorder]
#先序遍历和中序遍历的右子树
right_tree_pre = preorder[1+curr_root_index_in_inorder:]
right_tree_in = inorder[1+curr_root_index_in_inorder:]
#采用递归方法创建左子树
rootNode.left = self.buildTree(left_tree_pre,left_tree_in)
#采用递归方法采用右子树
rootNode.right = self.buildTree(right_tree_pre,right_tree_in)
return rootNode
2.删除数组中重复的数
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
"""
用两个指针,指向第一个和第二个元素,如果他们相等,删除第二个元素。指针还指向原来的位置,继续比较。不等的话,两个指针位置都加一
"""
if not nums:
return 0
left,right =0,1
while right<len(nums):
if nums[left]==nums[right]:
nums.pop(right)
else:
left,right = right,right+1
return len(nums)
80. 删除排序数组中的重复项 II
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
示例 1:
给定 nums = [1,1,1,2,2,3],
函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。
你不需要考虑数组中超出新长度后面的元素。
示例 2:
给定 nums = [0,0,1,1,1,1,2,3,3],
函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。
你不需要考虑数组中超出新长度后面的元素。
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i,count = 1,1 # 用来计算数组中元素出现的个数
while i < len(nums):
if nums[i - 1] == nums[i]:
count += 1
if count >= 3:
nums.pop(i)
i-=1#保持i的值不变
else:
count = 1
i+=1
return len(nums)
82. 删除排序链表中的重复元素 II
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head:
return None
#思路:构造一个新的链表new_head用来存储不重复的数
#为head链表添加一个空头,保证第一个数也能存入到新的链表new_head中,
#存入的条件,该数的前后都没有与之相等的数
new_head=new=ListNode(None)
cur = ListNode(None)
cur.next = head
while cur.next.next:
if cur.val!=cur.next.val!=cur.next.next.val:
new.next = ListNode(cur.next.val)
new = new.next
# print(cur.next.val)
cur = cur.next
#cur的指针最后走到了倒数第二位数,这个时候我们要比较倒数第一个是否等于当前的数
if cur.val!=cur.next.val:
new.next =ListNode(cur.next.val)
new = new.next
# print(cur.next.val)
# new.next=ListNode(None)
return new_head.next
83. 删除排序链表中的重复元素
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
#思路创建两个指针,一个快指针和一个慢指针,如果快指针的值和慢指针的值相等的话,删除快指针当前的值,让慢指针指向快指针的下一个值
if not head:
return None
if not head.next:
return head
pre = head
cur = head.next
while cur:
if pre.val==cur.val:
pre.next = cur.next
cur= cur.next
else:
pre,cur = cur,cur.next
return head
55.二叉树的深度
第一种方法:层序遍历(bfs)
参考链接:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/solution/mian-shi-ti-55-i-er-cha-shu-de-shen-du-xian-xu-bia/
if not root: return 0
queue, res = [root], 0
while queue:
tmp = []
for node in queue:
if node.left: tmp.append(node.left)
if node.right: tmp.append(node.right)
queue = tmp
res += 1
return res
第二种方法递归
def maxDepth(self, root: TreeNode) -> int:
#采用递归
if not root:
return 0
def dfs(root,step):
if not root:
return step
step+=1
left = dfs(root.left,step)
right = dfs(root.right,step)
return max(step,left,right)
return dfs(root,0)
655.输出二叉树
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def printTree(self, root: TreeNode) -> List[List[str]]:
#先求出二叉树的高度
if not root:
return [[]]
def length(root):
if not root:
return 0
queue,res = [root],0
while queue:
tmp = []
for node in queue:
if node.left:tmp.append(node.left)
if node.right:tmp.append(node.right)
queue = tmp
res+=1
return res
m = length(root)
n = 2**m-1
cur = [[""]*n for _ in range(m)]
#采用递归的进行插入数据,采用分支的办法进行数据的填充
def fill(cur,root,i,start,end):
if not root:
return
cur[i][(start+end)//2] = str(root.val)
#填充左子树
fill(cur,root.left,i+1,start,(start+end)//2)
#填充右子树
fill(cur,root.right,i+1,(start+end+1)//2,end)
fill(cur,root,0,0,n)
return cur
136.只出现一次的数字
调用python中collections模块
class Solution:
def singleNumber(self, nums: List[int]) -> int:
import collections
for key,value in collections.Counter(nums).items():
if value ==1:
return key
题目中要求具有线性时间复杂度
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return reduce(lambda x, y: x ^ y, nums)
class Solution {
public int singleNumber(int[] nums) {
int single = 0;
for (int num : nums) {
single ^= num;
}
return single;
}
}