当前位置:网站首页>Moore vote, leetcode169, leetcode229
Moore vote, leetcode169, leetcode229
2022-06-26 10:42:00 【MervynLammm】
Moore voted
A preliminary understanding
Topics involved
LeetCode 169 - Majority Element
Description of algorithm
In a given array , Find out if there are more than n/2
The elements of . The title assumes that there must be an element that appears more than n/2
Time .
Ideas
Delete two different elements in the array each time , After traversing the array , The remaining elements must be more than n/2
Secondary elements .
- Define the candidate
cand
Give any value 、 Countercount
Initialize to0
- Traversal array
- If
count
The value is0
, takecand
Assign to the current valuenums[i]
,count
The assignment is1
- If
count
Values are not for0
, And current valuenums[i]
be equal tocand
Value , becount
Add one , otherwisecount
Minus one
- If
- After traversal ,
cand
That is, the element
count
Subtracting one can be understood as deleting the current element in the array nums[i]
And a cand
The elements of , When count
Element is 0
It can be understood that all the preceding numbers have offset each other .
Code implementation
public int majorityElement(int[] nums) {
int cand = 0;
int count = 0;
for (int i = 0; i< nums.length; ++i) {
if (count == 0) {
count = 1;
cand = nums[i];
continue;
}
if (cand == nums[i]) count++;
else count--;
}
return cand;
}
Advanced
Topics involved
LeetCode 229 - Majority Element II
Ideas
The topic should be found to occur more than n/3
The elements of , But there is no guarantee that the element will exist .
Delete three different elements at a time , Last of all ( Two at most ) Must be the one who appears the most , But not necessarily greater than n/3
Time , You also need to iterate over whether the array count is greater than n/3
. Thus more than n/k
The elements of ( most k-1
individual ).
- Define the candidate
cand1
andcand2
Give any value 、 Countercount1
andcount2
Initialize to0
- Traverse the array to find possible elements for the first time
- If the current value
nums[i]
be equal tocand1
orcand2
Value , Correspondingcount1
orcount2
Add one - If
count1
orcount2
The value is0
, takecand1
orcand2
Assign to the current valuenums[i]
, Correspondingcount
assignment1
- otherwise
count1
andcount2
All minus one
- If the current value
count1
andcount2
The assignment is0
- Traverse the array for the second time to determine whether the condition is met ( The frequency of occurrence is greater than
n/3
)- Judge
nums[i]
Whether or notcand1
orcand2
equal , Equal corresponds tocount
Add one
- Judge
- Eventually greater than
n/3
The element of the degree is the desired
Code implementation
public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<>();
if (nums.length == 0) return result;
int cand1 = 0;
int cand2 = 0;
int count1 = 0;
int count2 = 0;
for (int i = 0; i < nums.length; ++i) {
if (cand1 == nums[i]) {
count1++;
continue;
}
if (cand2 == nums[i]) {
count2++;
continue;
}
if (count1 == 0) {
count1 = 1;
cand1 = nums[i];
continue;
}
if (count2 == 0) {
count2 = 1;
cand2 = nums[i];
continue;
}
count1--;
count2--;
}
count1 = 0;
count2 = 0;
for (int i = 0; i < nums.length; ++i) {
if (cand1 == nums[i]) count1++;
else if (cand2 == nums[i]) count2++;
// must do else if, because cand1 and cand2 It could be the same element
}
if (count1 > (nums.length)/3) result.add(cand1);
if (count2 > (nums.length)/3) result.add(cand2);
return result;
}
Reference material
Moore voting algorithm ( Boyer-Moore Voting Algorithm)
边栏推荐
- Blog post index summary --c #
- Is it safe to open an account in the school of Finance and business?
- AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
- String constant pool, class constant pool, and runtime constant pool
- Introduction to libmagic
- 創建對象的時候堆內存的分配
- Pytest configuration file
- appliedzkp zkevm(8)中的Plookup Table
- 【無標題】
- SSH, SCP command appears permission denied, please try again solution
猜你喜欢
Using foreach to loop two-dimensional array
Problems encountered in the application and development of Hongmeng and some roast
How to start the learning journey of webrtc native cross platform development?
Cmake / set command
MySQL 9th job - connection Query & sub query
开发者,微服务架构到底是什么?
MySQL第十二次作业-存储过程的应用
利用foreach循环二维数组
Concise course of probability theory and statistics in engineering mathematics second edition review outline
MySQL第八次作业
随机推荐
Search engine advanced search method records
Flutter and native communication (Part 1)
MySQL第五章总结
The fourteenth MySQL operation - e-mall project
Execute Lua script in redis
Concise course of probability theory and statistics in engineering mathematics second edition review outline
Postman入门教程
MySQL 13th job - transaction management
Oracle sqlplus 查询结果显示优化
The sixth MySQL job - query data - multiple conditions
【软件项目管理】期末复习知识点整理
Servlet learning notes II
Global and Chinese market of amateur football helmets 2022-2028: Research Report on technology, participants, trends, market size and share
Opencv image processing - grayscale processing
Leetcode intermediate node of linked list
Flutter与原生通信(上)
8-图文打造LeeCode算法宝典-最小栈与LRU缓存机制算法题解
35 qlineedit control synthesis example
SQL Server 基础介绍整理
String class intern() method and string constant pool