牛客210303

牛客21/03/03

NC78 反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。

示例1

输入
{1,2,3}
返回值
{3,2,1}
题解1:利用python的赋值特性
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        if not pHead:
            return None
        # 如果头结点不为空就进入循环
        root = None
        # 先新建一个空指针
        while pHead:
            pHead.next,root,pHead=root,pHead,pHead.next
            # 利用python的赋值特性,切忌拆成三行写
        return root
题解2:非递归
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        cur = pHead
        pre = None
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre
    #原理本质上和题解一是一样的,只是换成了非递归的写法
题解3:递归
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        if not pHead:
            return None
        if not pHead.next:
            return pHead
        headNode = self.ReverseList(pHead.next)
        pHead.next.next = pHead
        pHead.next = None
        return headNode

NC140 排序

题目描述

给定一个数组,请你编写一个函数,返回该数组排序后的形式。

示例1
输入
[5,2,3,1,4]
返回值
[1,2,3,4,5]
示例2
输入
[5,1,6,2,5]
返回值
[1,2,5,5,6]
备注:
数组的长度不大于100000,数组中每个数的绝对值不超过10^9109
题解
class Solution:
    def MySort(self , arr ):
        # write code here
        arr = sorted(arr)
        return arr

NC04 判断链表是否有环

题目描述

判断给定的链表中是否有环。如果有环则返回true,否则返回false。

你能给出空间复杂度[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E9rD0y17-1614743785663)(https://www.nowcoder.com/equation?tex=O(1)]%5C)的解法么?

题解
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

#
# 
# @param head ListNode类 
# @return bool布尔型
#
class Solution:
    def hasCycle(self , head ):
        # write code here
        if not head:
            return False
        cur = head
        while cur:
            curn = cur.next
            if curn == head:
                return True
            # 判断是否为环
            else:
                cur.next = head
                cur = curn
        return False

NC45 实现二叉树先序,中序和后序遍历

题目描述

分别按照二叉树先序,中序和后序打印所有的节点。

输入
{1,2,3}
返回值
[[1,2,3],[2,1,3],[2,3,1]]
备注:

n ≤ 1 0 6 n \leq 10^6 n106

题解1:一次递归
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
# 
# @param root TreeNode类 the root of binary tree
# @return int整型二维数组
#
class Solution:
    def threeOrders(self , root ):
        # write code here
        preorder, inorder, postorder = [], [], []
        def find(root):
            if not root:
                return None
            preorder.append(root.val)
            find(root.left)
            inorder.append(root.val)
            find(root.right)
            postorder.append(root.val)
        find(rooputongjiefa3t)
        return [preorder, inorder, postorder]
题解2:递归三次,普通解法
class Solution:
    def threeOrders(self , root ):
        # write code here
        pre_order, in_order, post_order = [], [], []
        def preorder(root):
            # 先序
            if not root:
                return None
            pre_order.append(root.val)
            preorder(root.left)
            preorder(root.right)
        def inorder(root):
            # 中序
            if not root:
                return None
            inorder(root.left)
            in_order.append(root.val)
            inorder(root.right)
        def postorder(root):
            # 后序
            if not root:
                return None
            postorder(root.left)
            postorder(root.right)
            post_order.append(root.val)
        
        # 调用函数
        preorder(root)
        inorder(root)
        postorder(root)
        return [pre_order, in_order, post_order]

NC119 最小的K个数

题目描述

给定一个数组,找出其中最小的K个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。如果K>数组的长度,那么返回一个空的数组

输入
[4,5,1,6,2,7,3,8],4
返回值
[1,2,3,4]
题解
# -*- coding:utf-8 -*-
class Solution:
    def GetLeastNumbers_Solution(self, tinput, k):
        # write code here
        if tinput == [] or k > len(tinput):
            return[]
        tinput.sort()
        return tinput[:k]

NC15 求二叉树的层序遍历

题目描述

给定一个二叉树,返回该二叉树层序遍历的结果,(从左到右,一层一层地遍历)
例如:
给定的二叉树是{3,9,20,#,#,15,7},
img
该二叉树层序遍历的结果是
[
[3],
[9,20],
[15,7]
]

示例1
输入
{1,2}
返回值
[[1],[2]]
示例2
输入
{1,2,3,4,#,#,5}
返回值
[[1],[2,3],[4,5]]
题解
class Solution:
    def levelOrder(self , root ):
            # write code here
            if not root: 
                return []  # 特殊情况,root为空直接返回
            from collections import deque
            # 下面就是BFS模板内容,BFS关键在于队列的使用
            layer = deque()
            layer.append(root)  # 压入初始节点
            res = []  # 结果集
            while layer:
                cur_layer = []  
                # 临时变量,记录当前层的节点
                for _ in range(len(layer)):  
                # 遍历某一层的节点
                    node = layer.popleft()  
                    # 将要处理的节点弹出
                    cur_layer.append(node.val)
                    if node.left:  
                    # 如果当前节点有左右节点,则压入队列,根据题意注意压入顺序,先左后右
                        layer.append(node.left)
                    if node.right:
                        layer.append(node.right)
                res.append(cur_layer) 
                # 某一层的节点都处理完之后,将当前层的结果压入结果集
            return res
模板1:不确定当前遍历到哪一层
while queue 不空:
    cur = queue.pop()
    for 节点 in cur的所有相邻节点:
        if 该节点有效且未访问过:
            queue.push(该节点)
模板2:确定当前遍历到哪一层
level = 0
while queue 不空:
    size = queue.size()
    while (size --) {
        cur = queue.pop()
        for 节点 in cur的所有相邻节点:
            if 该节点有效且未被访问过:
                queue.push(该节点)
    }
    level ++;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值