LeetCode 169. Majority Element

原题链接在这里:https://leetcode.com/problems/majority-element/

题目:

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

题解:

Method 1:最容易想到的就是用HashMap 计数,数值大于n/2(注意不是大于等于而是大于),就是返回值。Time Complexity: O(n). Space: O(n).

Method 2: 用sort, 返回sort后array的中值即可. Time Complexity: O(n*logn). Space: O(1).

Method 3: 维护个最常出现值,遇到相同count++, 遇到不同count--, count为0时直接更改最常出现值为nums[i]. Time Complexity: O(n). Space: O(1).

AC Java:

复制代码
 1 public class Solution {
 2     public int majorityElement(int[] nums) {
 3         /*
 4         //Method 1, HashMap
 5         HashMap<Integer, Integer> map = new HashMap<>();
 6         for(int i = 0;i<nums.length; i++){
 7             if(!map.containsKey(nums[i])){
 8                 map.put(nums[i],1);
 9             }else{
10                 map.put(nums[i],map.get(nums[i])+1);
11             }
12         }
13         
14         Iterator<Integer> it = map.keySet().iterator(); //Iterate HashMap
15         while(it.hasNext()){
16             int keyVal = it.next();
17             //There is an error here: Pay attentation, it is ">", but not ">="
18             //If we have three variables [3,2,3], ">=" will also return 2, 1>=3/2
19             if(map.get(keyVal) > nums.length/2){
20                 return keyVal;
21             }
22         }
23         
24         return Integer.MIN_VALUE;
25         */
26         
27         /*Method 2, shortcut
28         Arrays.sort(nums);
29         return nums[nums.length/2];
30         */
31         
32         //Method 3
33         if(nums == null || nums.length == 0)
34             return Integer.MAX_VALUE;
35         int res = nums[0];
36         int count = 1;
37         for(int i = 1; i< nums.length; i++){
38             if(res == nums[i]){
39                 count++;
40             }else if(count == 0){
41                 res = nums[i];
42                 count = 1;
43             }else{
44                 count--;
45             }
46         }
47         return res;
48         
49     }
50 }
复制代码

AC C++:

复制代码
 1 class Solution {
 2 public:
 3     int majorityElement(vector<int>& nums) {
 4         if(nums.size() == 0){
 5             return INT_MAX;
 6         }
 7 
 8         int res = nums[0];
 9         int count = 1;
10         for(int i = 1; i < nums.size(); i++){
11             if(nums[i] == res){
12                 count++;
13             }else if(count == 0){
14                 res = nums[i];
15                 count = 1;
16             }else{
17                 count--;
18             }
19         }
20 
21         return res;
22     }
23 };
复制代码

跟上Majority Element II.

类似Check If a Number Is Majority Element in a Sorted Array.

posted @   Dylan_Java_NYC  阅读(283)  评论(0)    收藏  举报
编辑推荐:
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
阅读排行:
· .NET周刊【4月第1期 2025-04-06】
· 国产的 Java Solon v3.2.0 发布(央企信创的优选)
· centos停服,迁移centos7.3系统到新搭建的openEuler
· 如何0基础学stm32?
· Python日志模块Logging使用指北
点击右上角即可分享
微信分享提示