当前位置:网站首页>[LeetCode] 多数元素【169】
[LeetCode] 多数元素【169】
2022-07-02 22:06:00 【山茶花开时。】
问题: 给定一个大小为n的数组nums,返回其中的多数元素(多数元素是指在数组中出现次数大于n/2的元素)
你可以假设数组是非空的,并且给定的数组总是存在多数元素
示例1
输入: nums = [3,2,3]
输出: 3
示例2
输入: nums = [2,2,1,1,1,2,2]
输出: 2
Python3解题
# 解法1
def majorityElement(nums):
length = len(nums) / 2
set_nums = set(nums)
for i in set_nums:
if nums.count(i) > length:
return i
# 解法2
import collections
def majorityElement(nums):
counts = collections.Counter(nums)
return max(counts.keys(), key=counts.get)
majorityElement([3,2,3]) # 3
majorityElement([2,2,1,1,1,2,2]) # 2涉及知识点:
[Python] set()函数
https://blog.csdn.net/Hudas/article/details/122882024
[Python] count()方法
https://blog.csdn.net/Hudas/article/details/122941544
边栏推荐
- Build your own website (22)
- C language, to achieve three chess games
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- Market Research - current situation and future development trend of preclinical medical device testing service market
- Utilisation de simpletk - 4. Question étrange
- Regular expression (2)
- How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
- U++ 学习笔记 堆
- 对象与对象变量
- What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
猜你喜欢

Simpleitk use - 4 Strange question

Share how to make professional hand drawn electronic maps

kubernetes 使用主机名将 pod 分配在指定节点上

PMP项目整合管理
![[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)](/img/07/6f2dfb543cb0ab4f27169da7e6ad07.jpg)
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)

Hanoi Tower problem
![[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)](/img/4c/c8dae41fc2eb18b5153cf36861fc7d.jpg)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)

Reading experience of just because

Pointer and string

任务和特权级保护
随机推荐
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
NC50965 Largest Rectangle in a Histogram
New feature of go1.18: introduce new netip Network Library
U++ learning note pile
phpcms实现订单直接支付宝支付功能
杰理之充电拔出,无法触摸开机【篇】
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
U++ 原始内存 学习笔记
Radis:Linux上安装Redis(步骤)
杰理之如何测试按键的误触率【篇】
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
JS solution for obtaining the width and height of hidden elements whose display is none
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
杰理之、产线装配环节【篇】
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
Mathematical modeling -- graph and network models and methods (I)
Market Research - current market situation and future development trend of aircraft wireless intercom system
Ransack combined condition search implementation