题目要求: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.
我的解法:

 1 int majorityElement(vector<int> &num) {
 2     int key = num.back();
 3     int k = num.size();
 4     k--;
 5     int i = k-1;
 6     while (i >= k/2)
 7     {
 8         if (num[i] != key)
 9         {
10             num[i] = num[k-1];
11             k--;
12             key = num[k-1];
13             k--;
14             i=k-1;
15             continue;
16         }
17         i--;
18     }
19     return key;
20 }

分析:首先要知道一个定理,如果一个序列中存在一个出现率大于1/2的元素,我们记为mE,那么如果去掉这个序列中的两个不相同的元素,新序列中一定存在一个出现率大于1/2的元素,依然是mE。
该算法最差情况需要在i指向第一个元素的时候,也就是算法的复杂度最差是O(n),最好的情况需要n/2。

这个问题还可以直接考虑,通过map来计数,来获取所求的元素。代码如下:

 1 int majorityElement(vector<int> &num) {
 2     map<int, int> count;
 3     for (int i : num)
 4     {
 5         if (++count[i] >= num.size() / 2)
 6         {
 7             return i;
 8         }
 9     }
10 }

转载于:https://www.cnblogs.com/charleschiu/p/4338250.html

OJ 169 Majority Element相关推荐

  1. Leet Code OJ 169. Majority Element [Difficulty: Easy]

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  2. leetcode讲解--169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. [Easy] 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  4. [勇者闯LeetCode] 169. Majority Element

    [勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...

  5. leetcode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. 【LeetCode】169. Majority Element

    原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...

  7. Leetcode - 169. Majority Element (多数投票问题)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【LeetCode从零单排】No.169 Majority Element(hashmap用法)

    题目 Given an array of size n, find the majority element. The majority element is the element that app ...

  9. [LeetCode]: 169: Majority Element

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

最新文章

  1. Windows下Python环境搭建
  2. 服务器write后客户端响应,客户端解析服务器响应的multipart/form-data数据
  3. swift golang java,解决两数之和 (Javascript, Java, C#, Swift, Kotlin, Python,C++, Golang)
  4. docker-compose常用命令整理及使用示例
  5. 在window10 WSL 中编译 OpenJDK13
  6. vba 自动排序_给VBA字典键值排序,并提取需要的数据
  7. Mac安装sqlmap【亲测有用】
  8. 案例:Oracle 11g RAC 数据库连接数过高处理办法
  9. 不定高宽的元素居中的方法
  10. 2021年中国再生纱市场趋势报告、技术动态创新及2027年市场预测
  11. OFF文件格式_拔剑-浆糊的传说_新浪博客
  12. 学计算机专业1050显卡够不够,gtx1050显卡性能怎么样
  13. linux内存管理笔记(十一)---CMA
  14. 公钥证书的颁发和使用
  15. 【Cheatsheet】详解git的各种操作
  16. MATLAB中均值、方差、标准差、协方差、相关性的计算
  17. HTML jQuery实现点赞功能(模仿CSDN的样式)
  18. SAP 采购订单收货时报错:对于采购订单xxxx无收货可能
  19. 读《杨振宁传》---谈taste
  20. C#设计模式——组合模式(Composite Pattern)

热门文章

  1. 4怎样判断动作是否执行_公众号交易时要怎样去判断粉丝是否真实的呢?
  2. C#求空间两点之间的距离
  3. JDBC之一:JDBC快速入门
  4. 剑指offer 顺时针打印矩阵
  5. Apache ab测试工具使用方法(无参、get传参、post传参)
  6. jstat的小伙伴:找出system.gc的调用的小工具
  7. 神圣的NLP!一文理解词性标注、依存分析和命名实体识别任务
  8. JavaWeb学习总结(十七):JSP中的九个内置对象
  9. Python汉诺塔问题
  10. Android--OkHttp理解系列(一)