当前位置:网站首页>[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
边栏推荐
- 影视随摘
- 图形视图框架
- [chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
- Socket socket c/s end process
- DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- [LeetCode] 多数元素【169】
- 服务器响应状态码
- Oracle cursor
- P1007 独木桥
猜你喜欢

Oracle-游标

地方经销商玩转社区团购模式,百万运营分享

Dahua cloud native load balancing article - the passenger flow of small restaurants has increased

Perceptron model and Application

Niuke: Dragon and dungeon games

Baidu AI Cloud - create a face recognition application

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

【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?

SimpleITK使用——3. 常见操作

附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
随机推荐
Jatpack------LiveData
Struct, bit segment, enumeration, union
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
杰理之、产线装配环节【篇】
U++ 学习笔记 堆
加油站[问题分析->问题转换->贪心]
U++ learning notes - relaxation
Film and television excerpts
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
Oracle cursor
E-commerce system microservice architecture
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
高并发介绍及应对
Perceptron model and Application
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
kubernetes 使用主机名将 pod 分配在指定节点上
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
How should programmers write logs
Objects and object variables