代码随想录leetcode200题之额外题目

news/2024/7/8 2:51:14 标签: 算法

目录

  • 1 介绍
  • 2 训练
  • 3 参考

1 介绍

本博客用来记录代码随想录leetcode200题之额外题目相关题目。

2 训练

题目1:1365. 有多少小于当前数字的数字

解题思路:二分查找。

C++代码如下,

class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& a) {
        vector<int> b = a;
        sort(b.begin(), b.end());
        vector<int> res;
        for (auto x : a) {
            auto iter = lower_bound(b.begin(), b.end(), x);
            int i = distance(b.begin(), iter);
            res.emplace_back(i);
        }
        return res;
    }
};

python3代码如下,

class Solution:
    def smallerNumbersThanCurrent(self, a: List[int]) -> List[int]:
        b = copy.deepcopy(a)
        b.sort()
        res = []
        for x in a:
            i = bisect.bisect_left(b, x)
            res.append(i)
        return res 

题目2:941. 有效的山脉数组

解题思路:模拟。

C++代码如下,

class Solution {
public:
    bool validMountainArray(vector<int>& a) {
        int n = a.size();
        if (n < 3) return false;
        if (!(a[0] < a[1])) return false;
        if (!(a[n-2] > a[n-1])) return false;
        int i = 0;
        while (i+1 < n && a[i] < a[i+1]) i += 1;
        int j = i;
        while (j+1 < n && a[j] > a[j+1]) j += 1;
        if (j != n-1) return false;
        return true;
    }
};

python3代码如下,

class Solution:
    def validMountainArray(self, a: List[int]) -> bool:
        n = len(a)
        if n < 3:
            return False 
        if not (a[0] < a[1]):
            return False
        if not (a[-2] > a[-1]):
            return False 
        i = 0 
        while i + 1 < n and a[i] < a[i+1]:
            i += 1 
        j = i 
        while j + 1 < n and a[j] > a[j+1]:
            j += 1 
        if j != n-1:
            return False 
        return True 

题目3:1207. 独一无二的出现次数

解题思路:模拟。

C++代码如下,

class Solution {
public:
    bool uniqueOccurrences(vector<int>& a) {
        unordered_map<int, int> cnt1;
        for (auto x : a) cnt1[x]++;
        unordered_map<int, int> cnt2;
        for (auto [k, v] : cnt1) {
            cnt2[v]++;
            if (cnt2[v] > 1) return false;
        }
        return true;
    }
};

python3代码如下,

class Solution:
    def uniqueOccurrences(self, a: List[int]) -> bool:
        cnt = collections.Counter(a)
        res = collections.Counter(cnt.values())
        for k in res:
            if res[k] > 1:
                return False 
        return True 

题目4:283. 移动零

解题思路:双指针。

C++代码如下,


python3代码如下,


3 参考

代码随想录


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

相关文章

SpringBoot应用配置桥接Prometheus入门

SpringBoot应用配置Prometheus步骤 SpringBoot应用依赖要求PrometheusGrafanaGrafana监控界面模板 SpringBoot应用依赖要求 <!-- 监控系统健康情况的工具 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot…

前端Debugger时复制的JS对象字符转JSON对象

前端debugger时&#xff0c;复制的对象在控制台输出时是如下格式&#xff0c;需要转换为对象格式来进行验证操作 bridgeId : 4118 createBy : null createTime : "2023-03-24 10:35:26" createUserId : 1 具体实现代码&#xff1a; // 转换transform (text) {l…

AGI 之 【Hugging Face】 的【Transformer】的 [ Transformer 架构 ] / [ 编码器 ]的简单整理

AGI 之 【Hugging Face】 的【Transformer】的 [ Transformer 架构 ] / [ 编码器 ]的简单整理 目录 AGI 之 【Hugging Face】 的【Transformer】的 [ Transformer 架构 ] / [ 编码器 ]的简单整理 一、简单介绍 二、Transformer 三、Transformer架构 四、编码器 1、自注意…

谈谈检测浏览器类型

前几天被问到如何检测浏览器类型&#xff0c;我突然发现我对此并不了解&#xff0c;之前的项目中也没有使用到过&#xff0c;只隐约记得通过一个自带的方法即可获取。所以今天特意来仔细补习一下。 核心&#xff1a;navigator.userAgent 1.正则表达式 2.引用外部库 3.判断浏…

可视化学习之pytorch可视化工具visdom

文章摘自详解PyTorch可视化工具visdom&#xff08;一&#xff09;-CSDN博客 模型训练过程中需要实时监听并可视化一些数据&#xff0c;如损失值loss&#xff0c;正确率acc等。在tensorflow中&#xff0c;使用的工具为tensorboard&#xff1b; 安装一下试试 1.安装 pip inst…

AlphaGo 背后的人工智能:机器学习和神经网络

文章目录 一、说明二、背景三、围棋游戏四、AlphaGo 算法五、神经网络六、AlphaGo 的未来七、人工智能的未来八、结论 一、说明 棋盘游戏围棋被视为人工智能最具挑战性的任务之一&#xff0c;因为它“复杂、基于模式且难以编程”。计算机程序 AlphaGo 战胜李世石成为人工智能和…

企业搭建知识库:解锁无限潜力的钥匙

在当今这个信息爆炸的时代&#xff0c;企业如何高效地管理、传播与利用知识&#xff0c;已成为衡量其竞争力的重要标尺。知识库&#xff0c;作为这一背景下的产物&#xff0c;正逐步成为企业不可或缺的数字资产。它不仅是一个自助式的数字门户&#xff0c;更是连接员工、客户与…