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

题目:

题解:

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
相关推荐
张张123y3 小时前
后端第十四课:Agent 记忆系统(让 Agent 记住上下文和用户,跨会话不健忘)
java·大数据·人工智能·python·agent·记忆
happylifetree7 小时前
Python67:核心语法-函数进阶-参数类型(函数作为参数)
python
小羊没烦恼!8 小时前
铁道部新客票系统设计(二)
开发语言·前端·数据库·python·oracle·c#·php
quantdash_cc9 小时前
从实时行情到价格观察表:用 Python 和 Pandas 构建策略输入数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
镜子AI9 小时前
镜子AI工程实践:用 Python 做 GEO 实体一致性审计,并自动生成 JSON-LD 结构化数据
人工智能·python·json
lvwangshu9 小时前
CF1882D Tree XOR 题解
动态规划·题解·贪心·dp·树形dp·换根dp·性质题·思维好题·推导题
文丑颜不良啊10 小时前
LeetCode201~225题目解析
java·leetcode
2601_9628857213 小时前
如何用 Python 计算 VaR 和 CVaR(在险价值)?
开发语言·python
李航198313 小时前
DeepSeekAI建模三 - 实战建模一组柜子
人工智能·python·计算机视觉·ai·ai编程
Nil20813 小时前
LeetCode 283移动零
数据结构·算法·leetcode