当前位置:网站首页>[leetcode] most elements [169]
[leetcode] most elements [169]
2022-07-02 22:52:00 【When camellia flowers bloom.】
problem : Given a size of n Array of nums, Return most of the elements ( Most elements refer to the number of occurrences in an array Greater than n/2 The elements of )
You can assume that the array is not empty , And there are always many elements in a given array
Example 1
Input : nums = [3,2,3]
Output : 3
Example 2
Input : nums = [2,2,1,1,1,2,2]
Output : 2
Python3 Problem solving
# solution 1
def majorityElement(nums):
length = len(nums) / 2
set_nums = set(nums)
for i in set_nums:
if nums.count(i) > length:
return i
# solution 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]) # 2Knowledge points involved :
[Python] set() function
https://blog.csdn.net/Hudas/article/details/122882024
[Python] count() Method
https://blog.csdn.net/Hudas/article/details/122941544
边栏推荐
- Source code analysis - lightweight asynchronous crawler framework Ruia
- PHP optimizes SQL queries in foreach
- 小鹏P7出事故,安全气囊未弹出,这正常吗?
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
- 解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
- 大话云原生之负载均衡篇-小饭馆客流量变大了
- [LeetCode] 存在重复元素【217】
- 【板栗糖GIS】arcscene—如何做出有高度的高程图
- Niuke: Dragon and dungeon games
- Commodity information management system (C language document version)
猜你喜欢

#include errors detected. Please update your includePath.

【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)

#include errors detected. Please update your includePath.

Simpleitk use - 4 Strange question

附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)

UE4 game architecture learning notes

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;)](/img/b7/0f543829b57cf2f2544efec4910c17.png)
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
![[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')

Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
随机推荐
UE4 游戏架构 学习笔记
牛客网:龙与地下城游戏
U++ 原始内存 学习笔记
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
小鹏P7出事故,安全气囊未弹出,这正常吗?
Unity publishes a method of webgl playing sound
UE4 UI自适应屏幕
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
Unity发布WebGL播放声音的一种方法
Socket socket c/s end process
Performance optimization - rigorous mode
php实现根据输入的年龄查询出生日期符合的数据
杰理之样机无触摸,拆机之后重新安装变正常【篇】
【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
Golang的学习路线
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed