原题链接:https://leetcode.com/problems/majority-element/description/

要求:

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.比较普通的想法,有一个标记,初始值为1,对应一个计数初始值为1.

遍历数组,若数组值和标记值相等,计数加一;否则计数减一,当计数为0时,更改标记值为当前数组值。代码如下:

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

2.首先将整个数组排序,由题意返回数组中间值即可。代码如下:

class Solution {
public:  int majorityElement(vector<int>& nums) {  sort(nums.begin(),  nums.end());  return nums[nums.size() / 2];  }
}; 

第二个方法代码量相对较少,容易想到,不过我比较了一下运行时间,还是第一个方法的时间少。

转载于:https://www.cnblogs.com/jialei-bupt/p/7259400.html

【LeetCode】169. Majority Element相关推荐

  1. 【LeetCode】169. Majority Element 解题小结

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

  2. leetcode讲解--169. Majority Element

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

  3. LeetCode - Easy - 169. Majority Element

    Topic Array Divide and Conquer Bit Manipulation Description https://leetcode.com/problems/majority-e ...

  4. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

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

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

  6. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)

    [LeetCode]309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: https: ...

  7. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  8. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  9. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

最新文章

  1. IE的box模型显示bug
  2. iOS-生成国际化包-配置App多语言支持
  3. python删除中文停用词_python词云 wordcloud+jieba生成中文词云图
  4. java web中验证码的实现
  5. 小米拒绝权限_小米手机MIUI12真有那么好吗?
  6. java swing图形界面开发设计器windowbuilder安装步骤详解
  7. android中获取时间
  8. 修改mysql root的秘密
  9. html 链接 vf,VFP中超链接实现方法
  10. 开启Mac原生NTFS支持
  11. CDN设置回源host的意义
  12. 大数据平台架构有哪些
  13. uni-app - MUMU模拟器模拟 iPad 尺寸开发(分辨率及DPI调整)
  14. Linux下邮箱客户端推荐
  15. 成为IT精英,我奋斗7年(震撼!正能量 转)
  16. WinDbg非常简单的调试dmp文件
  17. 图论中握手定理的详细解释
  18. 老调重弹-access注入过主机卫
  19. [旧博客]QQ旋风加速漏洞
  20. FAT32、NTFS和exFAT

热门文章

  1. OpenCV 加载图像、转换图像和保存图像
  2. ios开发之UIView和UIViewController
  3. python第三天課程:int, bool, str
  4. c. Litmxs找女友
  5. 【NOIP2015】斗地主 题解
  6. 关于vue项目中在js中引入图片问题
  7. bzoj - 2038: [2009国家集训队]小Z的袜子(hose)
  8. Tomcat7安装配置 for Ubuntu
  9. 例题:学习数据库查询。学生信息表的创建,主外键关系,以及45道题的查询实例。主要知识点在讲页45页,和讲页65页...
  10. Asp.Net的控件如何与Server交互