技术栈

Python | Leetcode Python题解之第169题多数元素

Mopes__2024-06-25 8:59

题目:

题解:

python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        count = 0
        candidate = None

        for num in nums:
            if count == 0:
                candidate = num
            count += (1 if num == candidate else -1)

        return candidate
上一篇:倾角传感器的精度和分辨率有什么区别?
下一篇:前端面试题(基础篇八)
相关推荐
圣保罗的大教堂
1 小时前
leetcode 3418. 机器人可以获得的最大金币数 中等
leetcode
chushiyunen
2 小时前
python中的@Property和@Setter
java·开发语言·python
禾小西
2 小时前
Java中使用正则表达式核心解析
java·python·正则表达式
yoyo_zzm
2 小时前
JAVA (Springboot) i18n国际化语言配置
java·spring boot·python
weixin_40809967
4 小时前
图片去水印 API 接口实战:网站如何实现自动去水印(Python / PHP / C#)
图像处理·人工智能·python·c#·php·api·图片去水印
yyk的萌
4 小时前
AI 应用开发工程师基础学习计划
开发语言·python·学习·ai·lua
_日拱一卒
5 小时前
LeetCode:最大子数组和
数据结构·算法·leetcode
qq_19697617
6 小时前
python的sql解析库-sqlparse
数据库·python·sql
:mnong
6 小时前
Superpowers 项目设计分析
java·c语言·c++·python·c#·php·skills
热门推荐
01GitHub 镜像站点022026年3月AI领域大事件:DeepSeek引领开源风暴03Qwen3.5-Omni与Qwen3.6模型全面解析(含测评/案例/使用教程)04Claude Code + GLM4.7 避坑指南:解决 Unable to connect to Anthropic services05AI 编程效率翻倍:Superpowers Skills 上手清单 + 完整指南06UV安装并设置国内源07让 Trae IDE 智能体 “读懂”文档 Excel+PDF+DOCX :mcp-documents-reader 工具使用指南08Mac 本地部署 OMLX + 通义千问 Qwen3.5-27B 保姆级教程09“wsl --install -d Ubuntu-22.04”下载慢,中国地区离线安装 Ubuntu 22.04 WSL方法(亲测2025年5月6日)10深扒 Claude Code Buddy 模式:一只仙人掌背后的确定性随机算法