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
相关推荐
炸薯条!27 分钟前
Python基础学习:文件操作详解(打开、读取、写入与路径)
java·服务器·python
jason_renyu2 小时前
Playwright 入门与实操:从安装、定位、断言到自动发博客,把浏览器自动化讲明白
运维·python·自动化·前端开发·浏览器自动化·playwright·e2e测试
维克兜率天2 小时前
【维克】布林带:通道类指标的“量化基石“
人工智能·python·算法
向科→向总工→空间动态计系统3 小时前
机器人大脑系统
开发语言·人工智能·python·机器人
染指11103 小时前
194.DeepAgents-在DeepAgents中使用自定义沙盒环境OpenSandBox
网络·人工智能·python·langchain·deepagents
吞下星星的少年·-·3 小时前
LeetCode 热题 100 最长连续序列(哈希表)
算法·leetcode·散列表
m0_646429974 小时前
LangGraph 学习笔记(一):StateGraph 与状态管理
python·langgraph
Dmz丶4 小时前
前端转 AI 100 天 Day 57:RAG 项目实战(六)——上线后迭代,从“能用”到“越用越好”
python
Pniubi4 小时前
力扣25.K个一组反转链表
算法·leetcode·链表
小小-猿5 小时前
用 Python 写昇腾算子:pyasc 是怎么回事
开发语言·python·昇腾·cann·pyasc