牛客网刷题笔记三 寻找第K大+两数之和+合并两个排序的链表+用两个栈实现队列

news/2025/2/23 8:59:53

算法题牛客网NC88 寻找第K大

题目:
在这里插入图片描述
思路就是做个排序,要求时间复杂度 O ( n log ⁡ n ) O(n\log n) O(nlogn),因此选用快排。代码:

class Solution:
    def quickSort(self, a, start, end):
        if start >= end:
            return
        val = a[start]
        low = start
        high = end
        while low < high:
            while low < high and a[high] >= val:
                high -= 1
            a[low] = a[high]
            while low < high and a[low] < val:
                low += 1
            a[high] = a[low]
        a[low] = val
        self.quickSort(a, start, low-1)
        self.quickSort(a, low+1, end)
        

    def findKth(self , a: List[int], n: int, K: int) -> int:
        # write code here
        self.quickSort(a, 0, n-1)
        return a[-K]

NC61 两数之和

题目
在这里插入图片描述
比较简单,之前也做过,直接用的循环遍历,但本次要求时间复杂度为 O ( n log ⁡ n ) O(n\log n) O(nlogn),循环遍历会超时,参考了下解题思路,用字典做hashmap的方式,代码:

class Solution:
    def twoSum(self , numbers: List[int], target: int) -> List[int]:
        # write code here
        # for i in range(len(numbers)-1):
        #     for j in range(i+1, len(numbers)):
        #         if numbers[i] + numbers[j] == target:
        #             return [i+1, j+1]
        # return []
        hash_map = {}
        for i in range(len(numbers)):
            tmp = target - numbers[i]
            if tmp in hash_map:
                return [hash_map[tmp]+1, i+1]
            elif numbers[i] not in hash_map:
                hash_map[numbers[i]] = i
        return []

NC33 合并两个排序的链表

题目:
在这里插入图片描述

这个题也是很简单的类型,因为输入就已经排好序了,只要遍历一下链表就可以了。代码:

class Solution:
    def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
        # write code here
        if not pHead1 and not pHead2:
            return None
        if not pHead1:
            return pHead2
        if not pHead2:
            return pHead1
        newHead = pHead1 if pHead1.val < pHead2.val else pHead2
        newCur = newHead
        cur1 = pHead1.next if pHead1.val < pHead2.val else pHead1
        cur2 = pHead2 if pHead1.val < pHead2.val else pHead2.next
        while cur1 and cur2:
            if cur1.val < cur2.val:
                newCur.next = cur1
                cur1 = cur1.next
            else:
                newCur.next = cur2
                cur2 = cur2.next
            newCur = newCur.next
        if cur1:
            newCur.next = cur1
        if cur2:
            newCur.next = cur2
        return newHead

NC76 用两个栈实现队列

题目:
在这里插入图片描述
这个题不知道是不是有什么不给用的坑,反正我直接只用了一个栈,过于简单有些怀疑是不是理解有偏差。代码:

class Solution:
    def __init__(self):
        self.stack1 = []
        self.stack2 = []
    def push(self, node):
        # write code here
        self.stack1.append(node)
    def pop(self):
        return self.stack1.pop(0)
        # return xx

http://www.niftyadmin.cn/n/5194356.html

相关文章

和解电话(匿名电话)/情侣拉黑联系电话/虚拟号/虚拟中间号/拉黑联系项目代码

和解电话&#xff0c;又名匿名电话 使用中间号转接到被叫人&#xff0c;不显示呼叫人号码&#xff0c;类似美团隐私号 呼叫人A->中间号B->被叫人C 演示地址&#xff1a;微信打开(http://sms.test.4php.top/sms/phone) 实现代码如下 <section class"section&q…

【MySQL--->视图】

文章目录 [TOC](文章目录) 一、概念二、操作三、视图特性 一、概念 视图是一个由插叙结果组成的虚拟表,基于表查询结果得到的表叫做视图,被查询的表叫做基表.基表和视图进行更新操作会互相影响. 二、操作 创建视图 将dept和emp两个基表的查询结果作为视图 更新基表会影响视…

C#有关里氏替换原则的经典问题答疑

目录 理解创建类型与变量类型很关键 先来理解变量类型。 再来理解创建类型。 有了正确的理解再来看疑问 里氏替换原则是面向对象七大原则中最重要的原则。 语法表现&#xff1a;父类容器装子类对象。 namespace 里氏替换原则 {class GameObject { } class Player : GameO…

吐血整理,金融银行测试的“火“到底在哪里?银行测试真正实施...

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 银行里的软件测试…

力扣 hot100 最长连续序列 哈希去重 双指针

128. 最长连续序列 ⭐ AC code class Solution {public int longestConsecutive(int[] nums) {if (nums.length 0)// 特判为空的数组&#xff0c;返回0return 0; // set实现去重HashSet<Integer> set new HashSet<>();for (int x : nums)set.add(x);Object[] a…

【kafka】使用docker启动kafka

1.环境准备 docker拉取zookeeper镜像 docker pull zookeeper:3.4.14 创建zookeeper容器&#xff0c;默认端口号为2181 docker run -d --name zookeeper -p 2181:2181 zookeeper:3.4.14 拉取kafka镜像 docker pull wurstmeister/kafka:2.12-2.3.1 创键kafka容器&#xff…

心怀祖国放眼世界 爱国人士华国中应邀参加美国旧金山2023(APEC)峰会

据相关媒体美国旧金山报道:2023亚太经合组织&#xff08;APEC&#xff09;领导人非正式会议将于11月15日至17日在美国旧金山召开。11月11日&#xff0c;本年度APEC高级财政官员和部长会晤在旧金山率先启动&#xff0c;APEC CEO峰会将于11月14日至16日开幕。著名爱国人士、亚太一…

解决在pycharm中使用matplotlib画图问题

第一&#xff0c;再导入包后直接绘图出现&#xff1a; AttributeError: module backend_interagg has no attribute FigureCanvas表明版本不兼容&#xff0c;我们需要加入&#xff1a;matplotlib.use(‘TkAgg’) 导入函数就变成了&#xff1a; import matplotlib matplotlib.…