当前位置:网站首页>Leetcode169 detailed explanation of most elements
Leetcode169 detailed explanation of most elements
2022-07-26 00:03:00 【Aries by】
Previous blogs :
Leetcode1- Detailed explanation of the sum of two numbers
Leetcode2- Detailed explanation of the code for adding two numbers
Leetcode20- Valid parentheses explain
Leetcode21- Merge two ordered linked lists
Leetcode22- Generation of valid parentheses
Leetcode24- Exchange the nodes in the linked list in pairs
Leetcode27- Remove element details
Leetcode46- Full arrangement and detailed explanation
Leetcode49- Detailed explanation of heterotopic grouping of letters
Leetcode53- Maximum subarray and explanation
Leetcode56- Detailed explanation of merging interval
LeetCode57- Insert interval explanation
Leetcode77- Detailed explanation of combination
Leetcode78- Subset explanation
Leetcode90- A subset of II Detailed explanation
Leetcode94- Detailed explanation of middle order traversal of binary tree
Leetcode102- Detailed sequence traversal of binary tree
Leetcode107- Sequence traversal of binary tree II Detailed explanation
Catalog
subject
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 .
The frequency of occurrence is greater than n/2, It indicates that the number of elements accounts for more than half of the whole array
Example
Example 1
Input :nums = [3,2,3] Output :3
Example 2
Input :nums = [2,2,1,1,1,2,2] Output :2
analysis
Sequencing
Since most elements account for more than half , Then sort the whole array , The elements in the middle of the array are most elements

Hashtable
Traversing the entire array from beginning to end , Record the number of occurrences of each number ,key For the elements ,value Is the number of occurrences ,value The largest elements are most elements

Code
Sequencing
class Solution:
def majorityElement(self, nums: List[int]) -> int:
nums.sort() # Sort
half = len(nums) // 2
return nums[half]HashTable
class Solution:
def majorityElement(self, nums: List[int]) -> int:
mapping = {}
for num in nums:
if num not in mapping:
mapping[num] = 0
mapping[num] = mapping.get(num)+1
half = len(nums) // 2
for key in mapping.keys():
if mapping[key] > half:
return key
return -1边栏推荐
猜你喜欢

Redis extended data type (jump table /bitmaps/hyperloglog/geospatial)

What is parity? How to use C language?

ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve

Leetcode200-查找岛屿数量详解

Practical experience of pair programming

结对编程实践心得

NVIDIA cudnn learning

行为型模式之迭代器模式

安全文档归档软件

二叉树——112. 路径总和
随机推荐
Leetcode107-二叉树的层序遍历II详解
Bubble sort idea and Implementation
开放API生态系统面临的十个威胁
BGR and RGB convert each other
Yolov3 trains its own data set
Programming password guessing game
Demo of pointer function
YoloV4-tiny网络结构
初阶C语言 - 分支语句(if、switch)
Nacos 下线服务时报错 errCode: 500
老旧笔记本电脑变服务器(笔记本电脑+内网穿透)
The late Apple co-founder Steve Jobs was posthumously awarded the U.S. presidential medal of freedom
二叉树——101. 对称二叉树
Lua脚本编写Wireshark插件解析第三方私有协议
ShardingSphere数据分片
C language actual combat guessing game
“群魔乱舞”,牛市是不是结束了?2021-05-13
Are you still using your browser's own bookmarks? This bookmark plugin is awesome
【英雄哥七月集训】第 24天: 线性树
SQLZOO——Nobel Quiz