当前位置:网站首页>[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
边栏推荐
- Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
- Oracle cursor
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- Socket套接字C/S端流程
- SimpleITK使用——4. 奇怪的问题
- 【外刊】睡眠与减肥
- Unity发布WebGL播放声音的一种方法
- php实现根据输入的年龄查询出生日期符合的数据
- Market Research - current market situation and future development trend of aircraft audio control panel system
- Try to get property'num for PHP database data reading_ rows' of non-object?
猜你喜欢

SimpleITK使用——4. 奇怪的問題

Dynamic memory allocation (malloc calloc realloc free)

Utilisation de simpletk - 4. Question étrange

scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文

Simpleitk use - 4 Strange question
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register
![[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')](/img/02/67448df1817e8b34b654722df8ecd4.jpg)
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')

Oracle PL / SQL programming

Socket socket c/s end process

建立自己的网站(22)
随机推荐
LxC terminal login method
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
Phpcms realizes the direct Alipay payment function of orders
Market Research - current situation and future development trend of herringbone gear Market
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
数学建模——图与网络模型及方法(一)
原生js添加样式的方法
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
ArrayList analysis 2: pits in ITR, listiterator, and sublist
Mathematical modeling -- graph and network models and methods (I)
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
Market Research - current market situation and future development trend of total nutrition products
Market Research - current situation and future development trend of marine clutch Market
Struct, bit segment, enumeration, union
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Technological Entrepreneurship: failure is not success, but reflection is
Pointer array parameter passing, pointer parameter passing