当前位置:网站首页>[leetcode] sword finger offer 39 Numbers that appear more than half of the time in the array
[leetcode] sword finger offer 39 Numbers that appear more than half of the time in the array
2022-07-18 20:00:00 【Spring-_- Bear】
There is a number in an array that appears more than half the length of the array , Please find out the number .
You can assume that the array is not empty , And there are always many elements in a given array .
Example 1:
Input : [1, 2, 3, 2, 2, 2, 5, 4, 2]
Output : 2
Limit :
1 <= The length of the array <= 50000
Answer 1 :
/** * The finger of the sword Offer 39. A number that appears more than half the times in an array */
public int majorityElement(int[] nums) {
// Because the mode occurs more than half the length of the array , Then the midpoint element after sorting must be mode
Arrays.sort(nums);
return nums[nums.length / 2];
}
Explanation 2 :
/** * The finger of the sword Offer 39. A number that appears more than half the times in an array */
public int majorityElement(int[] nums) {
/* * Use a hash table to count the number of occurrences of each element , Returns the element with the most occurrences * Optimize : In order to avoid traversing the hash table, find the most frequent key, * Maintain the most frequent elements in the process of traversing the array res And the number of occurrences maxTimes, * initialization res and maxTimes For the first element in the array */
Map<Integer, Integer> integerMap = new HashMap<>();
int maxTimes = 1;
int res = nums[0];
for (int num : nums) {
// The current element appears for the first time
if (!integerMap.containsKey(num)) {
integerMap.put(num, 1);
} else {
int curNumTimes = integerMap.get(num) + 1;
if (curNumTimes > maxTimes) {
maxTimes = curNumTimes;
res = num;
}
integerMap.put(num, curNumTimes);
}
}
return res;
}
Answer 3 :
/** * The finger of the sword Offer 39. A number that appears more than half the times in an array */
public int majorityElement(int[] nums) {
/* * Moore voting : The value of Jizhong is +1, The unusual value is -1, Then the sum of all elements in the array must be greater than 0 * In the process of traversing the array, if the number of votes votes be equal to 0 Then assume that the current element is the mode mode, * If the current element is equal to mode mode The number of votes votes++, otherwise votes--, Then the final assumed number must be the mode */
int votes = 0;
int mode = 0;
for (int num : nums) {
if (votes == 0) {
mode = num;
}
votes += (num == mode ? 1 : -1);
}
return mode;
}

边栏推荐
猜你喜欢

Horizon 8 测试环境部署(8): App Volumes Managers 负载均衡配置

面试微服务

全网首发 nuScenes数据集网盘 + 下载方法

第一个大规模中文视频多模态相似度数据集

Change signal sampling rate and measurement mean frequency, power and bandwidth

Ci521 domestic 13.56MHz reader chip replaces cv520 compatible

3. Terminate thread

3.终止线程

Mina中的支付交易snark

Interview micro service
随机推荐
8. Island issue
Interview micro service
死锁预防、死锁避免、死锁检测
[200 routines OpenCV] 232. Méthode spectrale de caractérisation
实现一年的所有周当按钮
2.Markdown使用说明
synchronized和Lock的区别
Change signal sampling rate and measurement mean frequency, power and bandwidth
Zordle:基于ZKP的Wordle应用
2.创建线程
Kernel management of Jupiter notebook
Window10 Remote Desktop Connection prompt: "unable to locate the program input point: \u cxxframehandler4 in the dynamic link library" error solution
eventbus短暂使用
Singleton mode in QT: implement a singleton interface class
Use and principle of CAS
leetcode 45. Jump Game II (dp)
Ci521 domestic 13.56MHz reader chip replaces cv520 compatible
Mental poker revised learning notes
Deadlock prevention, deadlock avoidance, deadlock detection
5.线程分离